Android/기본

Linear Layout gravity

Bohemian life 2012. 4. 28. 19:34

<Gravity 정렬>


android:gravity 

자신 내부의 구성 요소의 위치 정의

ex)LinearLayout에 명시, 버튼의 위치 지정


 자신(layout)을 기준으로 해서 하위요소(View)를 배치


LinearLayout 위치

  android:gravity=""

     center : 정중앙

     center_vertical : 좌측중앙

     center_horizontal : 상단중앙


android:layout_gravity 

부모를 기준으로 자신의 위치 정의

ex)Button에 명시 , 레이아웃 안에서 Button의 위치 지정


부모(layout)를 기준으로해서 자신(View)을 배치


Button (View) 위치

android:layout_gravity=""



<?xml version="1.0" encoding="utf-8"?>
<!--
android:gravity = 자신 내부의 구성요소의 위치 정의
ex)LinearLayout에 명시,버튼의 위치 지정

android:layout_gravity = 부모를 기준으로 자신의 위치 정의
ex)Button에 명시, 레이아웃 안에서 Button의 레이블(문자열)의 위치 지정

-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal" >

<!--  
center :정중앙
center_vertical : 좌측 중앙
center_horizontal : 상단 중앙
 -->

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="정렬" />


</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" >

    
<!-- 
android:layout_gravity = 부모를 기준으로해서 자식을 배치
 -->    
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="정렬" 
        android:layout_gravity="center"
        />

</LinearLayout>

정중앙에 나오지 않는 이유

orientation vertical의 영향을 받아서


gravity 사용하여 정중앙에 배치해야됨



gravity

자신을 기준으로 하위요소 배치(전체 적인)



layout_gravity

부모를 기준으로 자신을 배치

layout          view