2016-06-28 130 views
1

所以我想在屏幕上有三個部分。頂級的「工具欄」 - 大概佔用了屏幕10%的東西。我將稍後填寫的數據佔用了屏幕的70%,最後是底部搜索欄的20%的屏幕。Layout_Weight給予意想不到的結果

我認爲這將是一件簡單的事情:3個線性佈局子項的線性佈局,其權重分別爲0.1,0.7和0.2。但是,這不起作用。當我更改某些視圖的layout_weight時,它會更改其他視圖佔用的屏幕數量。下面的代碼我在此刻:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

<LinearLayout 
    android:id="@+id/linearLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="0.95" 
    android:orientation="horizontal"> 

</LinearLayout> 

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

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:background="@android:color/transparent" 
    > 

</LinearLayout> 

</LinearLayout> 

這給了我第一個空的佈局,佔據了20%的屏幕(是的,有0.95的重量),第二個空的佈局佔用的80%屏幕和最終的佈局是空的。當我給最終版面設計一個0.1的權重時,它會給第一個版面留出更多的空間。我認爲我有一些流氓格式。

回答

0

layout_weight documentation

該屬性指定在它應該佔用的空間在屏幕上的術語「重要性」價值的看法。較大的重量值允許它擴大以填充父視圖中的任何剩餘空間。

換句話說,它表示佈局可以佔用的剩餘空間的百分比。
如果你不想按這些比例分配空間,那麼你應該首先爲你的LinearLayouts指定一個0的高度(表明所有的屏幕都是可用空間)。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

    <LinearLayout 
     android:id="@+id/linearLayout" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.1" 
     android:orientation="horizontal"> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.7" 
     android:orientation="horizontal"> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.2" 
     android:background="@android:color/transparent"> 

    </LinearLayout> 

</LinearLayout> 
+0

我需要學習閱讀 –