【问题标题】:Buttons fill vertical space按钮填充垂直空间
【发布时间】:2019-05-23 02:11:53
【问题描述】:

有人告诉我,我可以使用第二个线性布局,这样我的按钮就会拉伸以垂直填充剩余的空白空间。我不知道从这里去哪里。任何帮助将不胜感激。

<?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>

【问题讨论】:

    标签: android android-layout android-layout-weight


    【解决方案1】:

    我假设您希望按钮填满屏幕上的可用高度。在这种情况下,请在此处查看答案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,因为您使用的是重量。你也不需要xmlns:android="http://schemas.android.com/apk/res/android" 3 次。您只需要在父布局上使用它。

    最后,您的许多按钮都使用相同的样式。我建议你看看可能使用这些按钮的样式。您可以通过http://developer.android.com/guide/topics/ui/themes.html了解更多关于样式/主题的信息。

    【讨论】:

      【解决方案2】:

      我认为您正在寻找的功能是 android:weightSum。 以下是官方参考: http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum

      因此,要将表格布局扩展到全屏,您需要为 TableLayout 分配 3 的 weightSum,并将 1 的 android: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>
      

      【讨论】:

      • 这应该是一个答案,而不是一个评论。
      • 您好 Mr.Exception-al 到目前为止,我没有 50 名代表对他的问题发表评论以详细说明他的问题。你建议我做什么。没有什么?在它之上,您的 -ve 投票也无济于事。谢谢!
      • 嗨,Manu,很遗憾,本网站的规则规定您在这种情况下不应采取任何行动。
      • 感谢您让我知道这一点。将用我认为他正在寻找的东西更新我的答案。 :)
      • 同时我建议你删除这个答案(这样你就不会失去声誉)并在你想出一个可以帮助 OP 的解决方案时发布一个新的答案。