2013-03-18 150 views
0

我有这样的按钮,如下图所示,我希望他们安排他们,因此他们开始在布局的顶部,并在底部结束,也自动安排空间之间。怎么做?如何在布局中填充剩余空间?

enter image description here

我的XML代码:

<?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:background="@drawable/background"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="60dp" 
     android:orientation="vertical" > 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="360dp" 
     android:orientation="horizontal" > 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="50" 
      android:gravity="center" 
      android:orientation="vertical" 
      android:paddingLeft="10dp" 
      android:paddingRight="5dp" > 

      <Button 
       android:id="@+id/bA1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="A1" 
       android:background="@drawable/buttons" /> 

      <Button 
       android:id="@+id/bA2" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="A2" 
       android:background="@drawable/buttons" /> 

      <Button 
       android:id="@+id/bA3" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="A3" 
       android:background="@drawable/buttons" /> 

      <Button 
       android:id="@+id/bA4" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="A4" 
       android:background="@drawable/buttons" /> 

      <Button 
       android:id="@+id/bA5" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="A5" 
       android:background="@drawable/buttons" /> 

      <Button 
       android:id="@+id/bA6" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="A6" 
       android:background="@drawable/buttons" /> 

      <Button 
       android:id="@+id/bA7" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="A7" 
       android:background="@drawable/buttons" /> 

      <Button 
       android:id="@+id/bA8" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="A8" 
       android:background="@drawable/buttons" /> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="50" 
      android:gravity="top" 
      android:orientation="vertical" 
      android:paddingLeft="5dp" 
      android:paddingRight="10dp" > 

      <Button 
       android:id="@+id/bB1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="@drawable/buttons" 
       android:text="B1" /> 

      <Button 
       android:id="@+id/bB2" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="B2" 
       android:background="@drawable/buttons" /> 

      <Button 
       android:id="@+id/bB3" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="B3" 
       android:background="@drawable/buttons" /> 

      <Button 
       android:id="@+id/bB4" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="B4" 
       android:background="@drawable/buttons" /> 

      <Button 
       android:id="@+id/bB5" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="B5" 
       android:background="@drawable/buttons" /> 

      <Button 
       android:id="@+id/bB6" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="B6" 
       android:background="@drawable/buttons" /> 

      <Button 
       android:id="@+id/bB7" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="B7" 
       android:background="@drawable/buttons" /> 

      <Button 
       android:id="@+id/bB8" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="B8" 
       android:background="@drawable/buttons" /> 

     </LinearLayout> 

    </LinearLayout> 

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

</LinearLayout> 
+0

给他们一个重 – njzk2 2013-03-18 16:52:39

回答

0

使用重量。一些示例代码如下。

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_centerHorizontal="true" 
    android:orientation="vertical" 
    android:padding="48dp" 
    android:weightSum="4" > 

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

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

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

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

代码android:weightSum="4"会说使用的4 “重量”,并且每个子在这里使用1,体重,一procentual值(25%)的。你可以改变每个孩子的体重和体重,从而给每个孩子一个百分比的宽度或高度。使用0dp来操纵维度。

0

试试这个..它为我工作。

enter image description here

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:baselineAligned="false" 
    android:orientation="horizontal" 
    android:layout_weight="8" > 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     android:paddingLeft="10dp" 
     android:paddingRight="5dp" 
     android:weightSum="8" > 

     <Button 
      android:id="@+id/bA1" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@android:drawable/dialog_frame" 
      android:text="A1" /> 

     <Button 
      android:id="@+id/bA2" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@android:drawable/dialog_frame" 
      android:text="A2" /> 

     <Button 
      android:id="@+id/bA3" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@android:drawable/dialog_frame" 
      android:text="A3" /> 

     <Button 
      android:id="@+id/bA4" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@android:drawable/dialog_frame" 
      android:text="A4" /> 

     <Button 
      android:id="@+id/bA5" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@android:drawable/dialog_frame" 
      android:text="A5" /> 

     <Button 
      android:id="@+id/bA6" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@android:drawable/dialog_frame" 
      android:text="A6" /> 

     <Button 
      android:id="@+id/bA7" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@android:drawable/dialog_frame" 
      android:text="A7" /> 

     <Button 
      android:id="@+id/bA8" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@android:drawable/dialog_frame" 
      android:text="A8" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     android:paddingLeft="5dp" 
     android:paddingRight="10dp" 
     android:weightSum="8" > 

     <Button 
      android:id="@+id/bB1" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@android:drawable/dialog_frame" 
      android:text="B1" /> 

     <Button 
      android:id="@+id/bB2" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@android:drawable/dialog_frame" 
      android:text="B2" /> 

     <Button 
      android:id="@+id/bB3" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@android:drawable/dialog_frame" 
      android:text="B3" /> 

     <Button 
      android:id="@+id/bB4" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@android:drawable/dialog_frame" 
      android:text="B4" /> 

     <Button 
      android:id="@+id/bB5" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@android:drawable/dialog_frame" 
      android:text="B5" /> 

     <Button 
      android:id="@+id/bB6" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@android:drawable/dialog_frame" 
      android:text="B6" /> 

     <Button 
      android:id="@+id/bB7" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@android:drawable/dialog_frame" 
      android:text="B7" /> 

     <Button 
      android:id="@+id/bB8" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@android:drawable/dialog_frame" 
      android:text="B8" /> 
    </LinearLayout> 
</LinearLayout> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:orientation="horizontal" 
    android:background="@android:drawable/dark_header" 
    android:layout_weight="1" /> 

现在它看起来像 正如我添加页眉和页脚也。

enter image description here

+0

这是很好的,但它充满了我的整个屏幕,并推动了我的底层。 – user2083882 2013-03-18 17:47:16

+0

现在我编辑了我的答案并添加了页眉和页脚布局。如果它为你工作,请你接受我的答案。谢谢 – 2013-03-18 18:38:11