android - Android TableLayout 中的按钮宽度

标签 android

我有一个 TableLayout,底部有一个按钮。我的问题是即使我不希望按钮那么宽,按钮也会拉伸(stretch)以填满列的整个宽度。

alt text

XML 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:stretchColumns="*"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
   <TableRow android:layout_height="wrap_content"
        android:layout_width="fill_parent"> 
     <CheckBox android:id="@+id/check1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" 
      android:text="Some text 1"/>
     <CheckBox android:id="@+id/check2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Some text 1"/>
   </TableRow>
   <TableRow android:layout_height="wrap_content"
        android:layout_width="fill_parent">  
     <CheckBox android:id="@+id/check3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" 
      android:text="Even some more text 2"/>
     <CheckBox android:id="@+id/check4"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Even some more text 2"/>
   </TableRow> 
   <TableRow android:layout_height="wrap_content"
        android:layout_width="fill_parent"> 
     <CheckBox android:id="@+id/check5"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" 
      android:text="An even longer line of text 3"/>
     <CheckBox android:id="@+id/check6"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Another longer line of text 3"/> 
   </TableRow>

   <EditText
      android:id="@+id/myEditText1"
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"
      android:inputType="textMultiLine"
      android:singleLine="false"
      android:text="Enter some text"
    />

   <TableRow> 
   <Button android:id="@+id/SaveButton"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:text="Save">
  </Button> 
  </TableRow>       

</TableLayout>

即使我为按钮设置了明确的宽度,它也会忽略它。例如,如果我有:

   <Button android:id="@+id/SaveButton"
      android:layout_width="100sp"
      android:layout_height="100sp"
      android:text="Save">
  </Button> 

...按钮变高,但宽度仍会拉伸(stretch)以填充该列。

我希望按钮的宽度大约是其当前宽度的一半,并位于列的中央。 提前致谢!!

最佳答案

@mbaird 是对的:android:stretchColumns="*" 总是会拉伸(stretch)列的宽度。但是,您仍然可以通过将 FrameLayout 放入延伸列宽度的列中,然后将按钮放在其中,从而在列内获得所需的行为。然后,您可以根据自己的喜好调整按钮的大小,并将其置于 FrameLayout 的中心:

<TableRow>
    <FrameLayout
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/SaveButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingLeft="40dp"
            android:paddingRight="40dp"
            android:text="Save">
        </Button>
    </FrameLayout>
</TableRow>

关于android - Android TableLayout 中的按钮宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4575155/

相关文章:

android - 如何在不使用 Intent 发送电子邮件的情况下打开电子邮件客户端?

android - 对 Android 的 promise ?

android - TileProvider 方法 getTile - 需要将 x 和 y 转换为 lat/long

android - 如何在可组合物中使用刀柄注入(inject)单例

android - 获取Android API 23及以上版本的当前位置经纬度

Android - 如何为不同主题中的某些特定 View 指定特殊属性

android - 编译android源代码时出现C++错误

Android:动态更改ImageButton的背景并显示它

javascript - 背景图片占据整个屏幕

Android字符串资源——需要包含 '%'字符