2013-07-26 244 views
1

有人告訴我,我可以使用第二個線性佈局,使得我的按鈕可以拉伸以垂直填充剩餘的空白空間。我不確定該從哪裏出發。任何幫助,將不勝感激。按鈕填充垂直空間

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:orientation="horizontal" > 
<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:gravity="center"> 
    <TableRow> 
     <Button 
      android:id="@+id/one" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_gravity="fill_vertical" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="1" /> 
     <Button 
      android:id="@+id/two" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="2" /> 
    </TableRow> 
    <TableRow> 
     <Button 
      android:id="@+id/three" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_gravity="fill_vertical" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="3" /> 
     <Button 
      android:id="@+id/four" 
      android:text="4" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:gravity="center" 
      android:layout_weight="1" /> 
    </TableRow> 
    <TableRow> 
     <Button 
      android:id="@+id/five" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="5" /> 
     <Button 
      android:id="@+id/six" 
      android:text="6" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:gravity="center" 
      android:layout_weight="1" /> 
    </TableRow> 
</TableLayout> 
</LinearLayout> 
</LinearLayout> 

回答

2

我認爲你正在尋找的功能是android:weightSum。 下面是官方參考: http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum

因此,要擴大你的表格佈局,以全屏幕,你需要的3 weightSum分配給您的TableLayout和分裂的Android版本:1 layout_weight每個的TableRow。那會給你想要的結果。

所以你的XML佈局文件將是這樣的:

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

    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:weightSum="3" 
    android:gravity="center"> 
    <TableRow 
     android:layout_weight="1"> 
     <Button 
      android:id="@+id/one" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_gravity="fill_vertical" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="1" /> 
     <Button 
      android:id="@+id/two" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="2" /> 
</TableRow> 
<TableRow 
    android:layout_weight="1"> 
    <Button 
     android:id="@+id/three" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_gravity="fill_vertical" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:text="3" /> 
    <Button 
     android:id="@+id/four" 
     android:text="4" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:layout_weight="1" /> 
</TableRow> 
<TableRow 
    android:layout_weight="1"> 
    <Button 
     android:id="@+id/five" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:text="5" /> 
    <Button 
     android:id="@+id/six" 
     android:text="6" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:layout_weight="1" /> 
</TableRow> 
</TableLayout> 
</LinearLayout> 
</LinearLayout> 
+0

它應該是一個答案,而不是評論。 – Androiderson

+0

Hello先生,您好,我現在沒有50位代表發表評論他的問題來闡述他的問題。你所做的是什麼。沒有?在它上面你的-ve投票也沒有幫助事業。謝謝! – Manu

+0

嗨馬努,不幸的是,這個網站的規則指出,在這種情況下你應該什麼也不做。 – Androiderson

2

我假設你想要的按鈕來填充屏幕上的可用高度。在這種情況下,請查看這裏的答案Android TableLayout height does not fills the whole screen

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

    <TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" > 

     <TableRow 
      android:layout_height="0dp" 
      android:layout_weight="1" > 

      <Button 
       android:id="@+id/one" 
       android:layout_width="0dp" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:gravity="center" 
       android:text="1" /> 

      <Button 
       android:id="@+id/two" 
       android:layout_width="0dp" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:gravity="center" 
       android:text="2" /> 
     </TableRow> 

     <TableRow 
      android:layout_height="0dp" 
      android:layout_weight="1" > 

      <Button 
       android:id="@+id/three" 
       android:layout_width="0dp" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:gravity="center" 
       android:text="3" /> 

      <Button 
       android:id="@+id/four" 
       android:layout_width="0dp" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:gravity="center" 
       android:text="4" /> 
     </TableRow> 

     <TableRow 
      android:layout_height="0dp" 
      android:layout_weight="1" > 

      <Button 
       android:id="@+id/five" 
       android:layout_width="0dp" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:gravity="center" 
       android:text="5" /> 

      <Button 
       android:id="@+id/six" 
       android:layout_width="0dp" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:gravity="center" 
       android:text="6" /> 
     </TableRow> 
    </TableLayout> 

</LinearLayout> 

注意,錶行現在有

android:layout_height="0dp" 
android:layout_weight="1" 

這將使該行擴大,以填補剩餘的空間(每行獲得1 /屏幕高度3)。

我還將按鈕佈局寬度更改爲0dp,因爲您使用的是重量。你也不需要3次使用xmlns:android="http://schemas.android.com/apk/res/android"。你只需要它在父佈局。

最後,儘可能多的按鈕使用相同的樣式。我建議你看看可能使用這些按鈕的樣式。您可以通過http://developer.android.com/guide/topics/ui/themes.html瞭解更多關於款式/主題的信息。