2013-06-21 2 views
1

저는 Android에 익숙하지 않고 선형 레이아웃으로 작업하고 있습니다. 화면의 20 %를 차지하는 이미지를 배치하고 싶습니다. 여기에 코드가 있지만 작동하지 않습니다.android 용 LinearLayout의 layout_weight가 작동하지 않습니다.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/LinearLayout1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/sky" > 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="2" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:src="@drawable/schoolroad" /> 

</RelativeLayout> 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="8" > 
</RelativeLayout> 

</LinearLayout> 

결과적으로 왼쪽 레이아웃이 화면의 80 %를 차지합니다.

내 코드에 어떤 문제가 있습니까?

답변

6

당신은 당신의 LinearLayout

의 방향을 지정해야하고 당신이 무게를 사용할 때 당신은 당신의 취향의 해당 0dp하는 폭 (또는 높이)를 설정해야합니다.

방향이 수직 인 경우 무게를 사용할 때 높이를 0으로 설정해야합니다. 가중치를 사용할 때 너비가 가로보다 작 으면 0이어야합니다.

이 시도 : layout_weight를 포함하는 아이 뷰에 대한

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/sky" 

    android:orientation="vertical" 

    > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="2" > 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:src="@drawable/schoolroad" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="8" > 
    </RelativeLayout> 

</LinearLayout> 
0

사용 android:layout_width="0dp" 대신 fill_parent.

0

이 시도 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/LinearLayout1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:background="@drawable/sky" > 

    <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="2"> 

     <ImageView 
       android:id="@+id/imageView1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:src="@drawable/schoolroad" /> 

    </RelativeLayout> 

    <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="8"> 
    </RelativeLayout> 

</LinearLayout> 
관련 문제