안녕하세요 김구조 입니다.

한다한다 하다가 드디어 안드로이드 들어왔내요.

저만 그런지모르겠지만 GUI개념은 좀어려워요 안써버릇한 개념이여서 그런지는 모르겠지만...

하다보면 이해되니까 저같은 분들도 많은 도움 얻어가시길 바래요.

틀린내용이나 궁금하신점은 댓글로 피드백 주시면 감사하겠습니다



레이아웃의 종류

1. 리니어 레이아웃

 - 차일드뷰(요소)를 일렬로 배치하여 차곡차곡쌓아 정렬하는 방식으로 가장 많이쓰이고 책에서도 처음에 나오는 방식.

2. 프레임 레이아웃

 - 차일드를 배치하는 기준이 없고, 모든차일드는 프레임 최산단에 나타나게됨, 그래서 막넣게되면 덮어씌워짐으로 주의하여야함!

3. 렐리티브 레이아웃 ( 상대적인 레이아웃 )

  - 위젯과 위젯 사이에 규칙을 정해 배치하는 레이아웃 (ex. Textview 오른쪽에 Button을 넣기)

대표적으로 3가지를 사용하고

오늘은 리니어 레이아웃에 대해 알아보겠습니다. 



1. 정렬하기

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    >

</LinearLayout>

<LinearLayout android:baselineAligned="true"> 

리니어 레이아웃 기본설정중에 가장중요한 3가지이다

layout_width와 height 는 각각의 크기를 지정할 수 있는대

fill_parent로 전부 채우거나 wrap_content로 내용물의 크기를 지정할 수 있다.

orientation은 가로로 채울것인지 세로로 채울것인지 정하는 요소이며

horizontal은 가로로 , vertical은 세로로 채운다 


 vertical horizontal 

 


2. gravity속성 정렬 하기

중력이란뜻인데.. 게임이생각나는 용어내요 

내용물을 어느쪽에 배치할 것인가를 gravity속성으로 지정하게됩니다. 수평 수직방향에 대한 정렬방식을 지정할수 있습니다

TextView를 사용할 경우에는

<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="구조구조의 구조구조한 코딩"
android:textSize="30px"
android:textColor="#ffffff"
android:gravity="center|right" 
/>

이런식으로 center|right 로 설정하게되면


이런식으로 세로로 가운대 가로로 오른쪽 정렬이 이루어 지게 됩니다.

요소는 아래 표를 보고 참고하시고요




3 베이스 정렬
 

높이가 다른 차일드 뷰를 수평으로 나랑히 배치할때 사용하며, 바닥에 붙혀 정렬한다. 

크기가다른 요소들을 붙혀서 사용할 때 용이하다.

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:baselineAligned="true" > <!-- false로 할경우 정렬이 안된다. -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="김구조의 "
        android:textSize="10pt"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="구조구조한 "
        android:textSize="5pt"
        android:background="#0000ff"
        />
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="블로그 "
        android:textSize="20pt"
        android:typeface="serif"
        />

</LinearLayout>  


마진과 패딩

레이아웃에 요소가 다닥다닥 붙어있으면 안이쁘고 손으로 누를때 오작동의 가능성이 있기 때문에 여백을 띄워주어 잘정돈할 수 있다.

 

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="구조구조해!"
        />
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#00ff00"
        android:padding="15px"
     android:layout_margin="15px">
    <Button 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="구조구조해!!"
        />
    </LinearLayout>
    

</LinearLayout>

마진과 여백을 보여주기위해 안에 리니어 레이아웃을 하나 더 넣어서 표현해 보았다.

여기서 녹색부분 태두리와 액티비티 끝부분과 떨어져있는 걸 볼수있는대 저것을 마진이라하고

녹색부분이 버튼과 떨어진 패딩이다. 다른 그림으로 보면

  
다음그림과 같이 볼수 있다. 필요에 따라 패딩이나 마진으로 여백을 주어 표현이 가능하고, 저것을 얼마나 응용하나에 따라서 이쁜어플이 될 수 있으리라 생각한다.


안드로이드 첫글인데, 코드 수정하고 올리고 좀 이쁘게하려고했는대 생각처럼안되고 힘드내요.

리플하나 꼭 받아보고 싶은대 부족하거나 문제있으면 피드백 부탁드립니다.

그래야 제가 더 공부를 ㅠㅠ

 

+ Recent posts