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