Android TableLayout中的按钮宽度
我有一个底部有按钮的桌面布局。我的问题是,即使我不想让它有那么宽,按钮也会拉伸以填充整个列的宽度 XML如下所示:Android TableLayout中的按钮宽度,android,Android,我有一个底部有按钮的桌面布局。我的问题是,即使我不想让它有那么宽,按钮也会拉伸以填充整个列的宽度 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"
<?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>
。。。按钮变得更高,但宽度仍会拉伸以填充列
我希望按钮的宽度大约为当前宽度的一半,位于列的中心提前感谢我很确定
android:stretchColumns=“*”
总是会导致按钮像那样拉伸。您是否考虑过将按钮放在桌面布局之外
从文件中:
TableLayout的子级不能指定layout_width属性。宽度始终与父项匹配。但是,布局高度属性可以由子级定义;默认值为WRAP_CONTENT@mbaird是正确的:
android:stretchColumns=“*”
始终会拉伸列的宽度。但是,您仍然可以通过在列中放置一个框架布局来获得所需的列内行为,该布局可以拉伸列的宽度,然后将按钮放在其中。然后,您可以随意调整按钮的大小,并将其置于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>
此解决方案的问题在于,如果按钮位于列的外部,则很难将按钮置于列的中心位置。