2016-09-07 32 views
0

我有兩個不同的LinearLayout(垂直)。 1st LinearLayout有3個按鈕,分別命名爲btnX,btnY,btnZ。第二個LinearLayout有兩個名爲btnA,btnB的按鈕。 btnB頂端對齊應該遵循btnY。如果我添加一個新的按鈕btnX(假設),btnB的高度將會隨着圖片的增加而增加。你可以從中得到一個清晰的想法。如何在不同的linearLayout上使用另一個按鈕匹配按鈕高度?

enter image description here

+0

顯示您的佈局代碼的餘量頂部,你已經嘗試到目前爲止 –

+0

我把他們都在一個相對佈局不線性佈局,所以我可以通過一個補充,作爲一項規則,並在同一時間減少我的佈局深度(因而透支)。 –

+0

如果LinearLayout沒有達到您想要的效果,請考慮使用不同的佈局。 RelativeLayout可能正是你需要的。 –

回答

0

你可以使用佈局權重。 在你LinearLayouts,添加 android:weightSum=3

然後,分配這每個佈局: android:layout_weight=1

這樣一來,任何按鈕將肩負起高度的1/3,不管你有多少佈局都有。如果你想有一個按鈕拍攝高度的2/3,layout_weight設置爲2

0

最有效的解決方案是實現一個相對佈局...但你可以嘗試把你的按鈕B內的另一個獨立的LinearLayout,並把他們的RelativeLayout

0

內對此進行詳細說明,但我希望它能幫助。變化的寬度和高度,只要你喜歡,按鈕B的高度爲((按鈕A的高度)* 2)+按鈕

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<Button 
    android:id="@+id/btnZ" 
    android:layout_width="216dp" 
    android:layout_height="86dp" 
    style="@style/Widget.AppCompat.Button.Colored" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="24dp" 
    android:text="Button Z"/> 
<Button 
    android:id="@+id/btnY" 
    android:layout_width="216dp" 
    android:layout_height="86dp" 
    style="@style/Widget.AppCompat.Button.Colored" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="24dp" 
    android:text="Button Y" 
    android:layout_below="@+id/btnZ"/> 
<Button 
    android:id="@+id/btnX" 
    android:layout_width="216dp" 
    android:layout_height="86dp" 
    style="@style/Widget.AppCompat.Button.Colored" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="24dp" 
    android:text="Button X" 
    android:layout_below="@+id/btnY"/> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<Button 
    android:id="@+id/btnA" 
    android:layout_width="216dp" 
    android:layout_height="86dp" 
    style="@style/Widget.AppCompat.Button.Colored" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="24dp" 
    android:text="Button A"/> 
<Button 
    android:id="@+id/btnB" 
    android:layout_width="216dp" 
    android:layout_height="196dp" 
    style="@style/Widget.AppCompat.Button.Colored" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="24dp" 
    android:text="Button B" 
    android:layout_below="@+id/btnA"/> 

相關問題