CheckBox 未居中于 TableRow

CheckBox not centered in the TableRow

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

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

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

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>

我打赌你的 0dp 宽度设置为 match_parent,试试 wrap_content:

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />

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>