2011-05-11 3 views
4

모든 크기의 기기에 맞춰 layout_weight을 사용하여 Android 레이아웃을 작성하려고하는데 그 컴 포먼트를 이해하는 데 어려움이 있습니다.Android layout_weight comportment

layout_width/layout_height을 변경하면 layout_weight 구성에 영향을 주지만 그 방법은 알 수 없습니다.

예를 들어, 수직의 LinearLayout을 세 개의 내부로 나눈 뒤 LinearLayout의 위쪽과 아래쪽이 화면의 25 %를 채우고 가운데 50 %가 켜지도록하려는 경우, 내부 레이아웃의 내용에 의존해서는 안됩니다. 내가 fill_parent 또는 wrap_content에 내부 LinearLayoutlayout_height 속성을 설정해야한다,이를 위해

을 (A 내부 LinearLayout의 내용이 너무 큰 경우, 다른 레이아웃을 이동 안)?

감사합니다.

편집 : layout_weight는 레이아웃이 차지하는 크기에 반비례합니다.

3 예 :

무게 1/1/1 (I가 예상대로 작동)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
     <LinearLayout 
      android:id="@+id/layout1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#FF0000"/> //RED 
     <LinearLayout 
      android:id="@+id/layout2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#00FF00"/> //GREEN 
     <LinearLayout 
      android:id="@+id/layout3" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#0000FF"/> //BLUE 
</LinearLayout> 

결과 : enter image description here

무게 1/2/1 (왜, 오 왜?)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
     <LinearLayout 
      android:id="@+id/layout1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#FF0000"/> //RED 
     <LinearLayout 
      android:id="@+id/layout2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="2" 
      android:background="#00FF00"/> //GREEN 
     <LinearLayout 
      android:id="@+id/layout3" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#0000FF"/> //BLUE 
</LinearLayout> 

결과 : enter image description here

** 무게 3/2/3 (내가 1/2/1와 함께 할 entended 무엇) :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
     <LinearLayout 
      android:id="@+id/layout1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="3" 
      android:background="#FF0000"/> //RED 
     <LinearLayout 
      android:id="@+id/layout2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="2" 
      android:background="#00FF00"/> //GREEN 
     <LinearLayout 
      android:id="@+id/layout3" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="3" 
      android:background="#0000FF"/> //BLUE 
</LinearLayout> 

결과 : enter image description here

답변

1

당신은 설정해야합니다 세 가지 내부 레이아웃의 layout_height를 "fill_parent"로 변경 한 다음 가중치를 변경하여 원하는 모양으로 만듭니다. 예상 (문서화)로

+0

@Egor :이 작업을 시도했지만이 경우 layout_weight가 어떻게 작동하는지 이해할 수 없습니다. 1/1/1을 입력하면 세 부분이 동일 해집니다. 그러나 1/2/1을 넣으면 두 번째 아이콘이 보이지 않으며 첫 번째 레이아웃의 50 %와 세 번째 레이아웃의 50 %로 화면이 분할됩니다. 어떻게 작동합니까? – nbarraille

+0

@nbarraille : 문제가되는 레이아웃을 게시해야 특정 상황에서 무엇이 잘못되었는지 더 잘 판단 할 수 있습니다. 왜냐하면 공간은 지정된대로 배포해야하기 때문입니다. – dmon

+0

@dmon : 초기 게시물을 예제로 업데이트했습니다. – nbarraille

3

당신은 당신이해야 하지fill_parentmatch_parent도에 layout_height를 설정 작동하도록 layout_weight를 들어 layout_height = 0dp

관련 문제