본문 바로가기

공부/Android

[Android]layout_gravity, gravity의 차이 (+ layout_weight)

★ 두 줄 요약 

gravity는 레이아웃의 내부에서의 위치관계를 말하는 것이고,

layout_gravity는 해당위젯이 포함된 레이아웃의 내부에서 기준으로 위치를 표현하는 것이다.





XML파일에서 


버튼이나 텍스트와같은 위젯들과 Linearlayout 등에서


gravity와 layout_gravity가 헷갈렸다.


그래서 그냥 나 혼자 끄적거려본 결과 이해를 할 수 있었다.



우선 결과는 다음과 같다.




위 화면의 XML 코드는 다음과 같다.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.user.practice0323_03.MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="버튼1" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="버튼2" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:orientation="horizontal">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="버튼3" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="버튼4" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="버튼5" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="버튼6" />

</LinearLayout>

</LinearLayout>


이렇게 표현할 수 있다.


여기서 주목해야할 점은, 화면을 이루는 Linearlayout을 3개의 Linearlayout이 구성하고있다는 것이다.


여기서 각 Linearlayout의 내부 버튼들을 보면, Linearlayout에서 정 중앙에 위치해있음을 알 수 있다.


이것은 바로 각각의 Linearlayout에서 gravity값을 center로 설정했기 때문이다.


다시 말하자면 gravity는 레이아웃의 내부에서의 위치관계를 말하는 것이다. 


이것을 layout_gravity를 사용해서 표현하면 다음과 같은 코드로 표현할 수도있다.



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.user.practice0323_03.MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">

<Button
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="버튼1" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="버튼2" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal">

<Button
android:layout_marginLeft="70dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="버튼3" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="버튼4" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">

<Button
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="버튼5" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="버튼6" />

</LinearLayout>

</LinearLayout>

layout_gravity는 위젯이 포함된 레이아웃의 내부에서 기준으로 위치를 표현한다.


center라고 표현하면 좌우를 기준으로 center에만 위치하지 위아래에서 center에 위치하지는 않는다.

버튼5가 들어있는 Linearlayout(버튼 바로 위 실선)에 붙어있다(=위,아래로 center에 위치하지는 않는다.)



위 코드와 이전 코드를 비교해보아 알 수 있듯이,


layout_gravity를 이용해서 전체적인 위치를 잡는게 훨씬 편한듯하다.


물론 지금 내가 경험하지 못한 예외적인 상황도 있겠지만, 지금까지는 이렇게 해야할 것 같다.



--------------------------------------4월 6일(목) 추가<출처>--------------------------------------


android:layout_weight




android:weightSum



'공부 > Android' 카테고리의 다른 글

[Android] Activity  (0) 2017.03.26
[Android] Intent, Activity 전환  (0) 2017.03.26
[Android] Lec02  (0) 2017.03.23
[Android] Lec 01  (0) 2017.03.23
[Android] 용어정리  (0) 2017.03.23