gpt4 book ai didi

android - CheckBox 未居中于 TableRow

转载 作者:行者123 更新时间:2023-11-30 00:08:07 26 4
gpt4 key购买 nike

我正在努力对齐下述布局中表格行中的复选框。我哪里出错了?

您可以在图片中看到复选框向左对齐,我希望它们位于中间。

我尝试了 layout-gravity 中的所有功能,也尝试将复选框放在另一个布局中,但没有任何帮助。

enter image description here

xml 看起来像这样:

<!--  TABLE  -->
<TableLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_top_margin"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">

<!-- HEADER -->
<TableRow android:background="@drawable/table_border_header">

<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:background="@drawable/table_border_header"
android:padding="4dp"
android:text="HEADER 1"
android:textAlignment="textStart"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
android:textStyle="bold" />

<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:background="@drawable/table_border_header"
android:padding="4dp"
android:text="HEADER 2"
android:textAlignment="center"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
android:textStyle="bold" />

<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:background="@drawable/table_border_header"
android:padding="4dp"
android:text="HEADER 3"
android:textAlignment="center"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
android:textStyle="bold" />

</TableRow>

<!-- SECOND ROW -->
<TableRow android:background="@drawable/table_border">

<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:background="@drawable/table_border"
android:padding="4dp"
android:text="SEED 1"
android:textAlignment="textStart"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium" />

<CheckBox
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:background="@drawable/table_border"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large" />

<CheckBox
android:layout_width="0dp"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:background="@drawable/table_border"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large" />

</TableRow>
</TableLayout>

最佳答案

the problem is that the Checkbox widget uses a regular TextView with the drawableLeft attribute, because it expects text to be shown as well. (This is why you see it centered vertically, but offset slightly to the left.)

您可以使用父布局来实现这一点:

            <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">

<CheckBox
android:id="@+id/checkbox_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

试试这个

    <TableRow android:background="@drawable/table_border">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/table_border"
android:text="SEED 1"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/table_border"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large" />
</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">

<CheckBox
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:background="@drawable/table_border"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large" />

</LinearLayout>


</TableRow>

关于android - CheckBox 未居中于 TableRow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48625496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com