2013-06-21 63 views
1

我是android新手,我正在使用線性佈局。我想放置佔用屏幕20%的圖像。這裏是代碼,但這不起作用。LinearLayout的layout_weight for android不起作用

<?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相應的方向。

所以如果你的方向是垂直的,你應該把高度設置爲零時使用重量。 如果橫向比使用權重時寬度應爲零。

試試這個:

<?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包含layout_weight孩子的意見。

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> 
相關問題