2013-03-18 50 views
0

我有兩種佈局。其實5,3垂直一個在另一個之下,在中間一個我有2個並排佈局。我需要他們是固定的大小,所以他們會改變。但是,只要我按下其中一個按鈕,它們就會改變大小,如下圖所示。如何讓他們不變?無論如何,這是我的代碼和圖片。如何製作固定的XML佈局?

Layout One Layout Two

<?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" 
    android:weightSum="20" > 

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

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

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

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

    </LinearLayout> 

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

</LinearLayout> 

回答

1

嘗試改變兩個內佈局,以這樣的:

選項#1:這將水平固定的佈局,不垂直

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:layout_weight="50" > 
</LinearLayout> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:layout_weight="50" > 
</LinearLayout> 

選項#2:這將修復雙向佈局

使用LinearLayouts的嵌套權重效率非常低。相反,請將您的根視圖更改爲RelativeLayout並擺脫權重。 請注意,這不是一個100%的完整的解決方案,但它一定會得到你開始。這將使第一LinearLayout在佈局的頂部,並在佈局底部的最後LinearLayout。中間的一個將始終在頂部視圖下方並在底部視圖下方。

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

    <LinearLayout android:id="@+id/top" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:orientation="vertical"> 
    </LinearLayout> 

    <LinearLayout android:id="@+id/middle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/top" 
     android:layout_above="@+id/bottom" 
     android:orientation="horizontal" > 

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

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

    </LinearLayout> 

    <LinearLayout android:id="@+id/bottom" 
     android:layout_alignParentBottom="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
    </LinearLayout> 
</RelativeLayout> 
+0

這工作了水平移動,它是固定的,但它還是挺大的verticaly。 – user2083882 2013-03-18 14:39:03

+0

查看編輯,我添加了第二個選項 – Sababado 2013-03-18 15:13:57

0

嘗試將layout_weight更改爲中間線性佈局的子線性佈局的0.5。