2016-04-01 51 views
0

嘗試使用LinearLayout在底部對齊按鈕,但只是在TextView下面進行對齊。使用LinearLayout將按鈕對齊到底部

要在底部設置按鈕,我使用android:layout_gravity="bottom"但仍沒有完成

的LinearLayout XML

<LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="0dp" 
     android:layout_height="match_parent"    
     android:orientation="vertical" > 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

     <TextView 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content"/> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content"/> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_gravity="bottom" 
      android:layout_height="wrap_content"> 

      <Button 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:layout_height="wrap_content" 
       android:text="Bottom" /> 

     </LinearLayout> 

</LinearLayout> 
+2

http://stackoverflow.com/questions/6575409/linearlayout-layout-gravity-bottom-not-working-on-horizo​​ntal-linearlayout –

+1

最好使用RelativeLayout的 –

+0

@MD我同意,但是請檢查以上鍊接 –

回答

4

將第二個線性佈局更改爲

<LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:orientation="horizontal" > 

這將使按鍵在底部,這種佈局將採取的剩餘空間

0
<LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="0dp" 
     android:layout_height="match_parent"    
     android:orientation="vertical" > 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

     <TextView 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content"/> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content"/> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_weight="1" 
      android:layout_width="fill_parent" 
      android:layout_gravity="bottom" 
      android:layout_height="wrap_content"> 

      <Button 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:layout_height="wrap_content" 
       android:text="Bottom" /> 

     </LinearLayout> 

</LinearLayout> 
0

同比不得不使用這樣的....

<RelativeLayout 
     android:id="@+id/footer" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:gravity="center"> 
     <Button 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:layout_height="wrap_content" 
       android:text="Bottom" /> 
    </RelativeLayout> 
+1

同意android:layout_alignParentBottom =「true」 –

0

我不能使用建議的答案,因爲我有3名垂直者倍數(每個特定權重)在一個水平的裏面。因此,最終使用了特定按鈕/小部件的margin top。到目前爲止工作正常。

不想更改爲RelativeLayout,因爲這意味着此場景中的許多更改。

Example Layout