2013-07-26 155 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了解更多关于款式/主题的信息。