如何在 table 布局中将视图组件放置在单元格的中心

How to place a view component in center of a cell in table layout

我有一个 table 五列布局,在最后一列有复选框 我想将复选框放在 table 布局中列的中心,任何一个都可以告诉我如何在 android.

中的 table 布局列的中心放置复选框

这是我的 xml:

 <TableLayout
        android:id="@+id/table_layout_manual_mode"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:shrinkColumns="5">

        <TableRow
            android:id="@+id/After_connection_heading_one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#673AB7" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="CHECK ON J14"
                android:textColor="#FFFFFF"
                android:textSize="20dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow1_manual"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:layout_weight="0.35"
                android:background="@drawable/cell_shape"
                android:gravity="center"
                android:text="@string/TP_15_Sno"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="20dp" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:layout_weight="1.5"
                android:background="@drawable/cell_shape"
                android:gravity="center"
                android:text="@string/TP_15_Description"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="20dp" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:layout_weight="1"
                android:background="@drawable/cell_shape"
                android:gravity="center"
                android:text="@string/Test_Point_15"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="20dp" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="1dp"
                android:layout_height="65dp"


                android:layout_weight="1"
                android:background="@drawable/cell_shape"
                android:gravity="center"
                android:text="@string/TP_15_Range"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="20dp" />

            <CheckBox
                android:id="@+id/cb_1"
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:layout_weight="1"
                android:background="@drawable/cell_shape"
                android:layout_gravity="center"
                />
        </TableRow>
</TableLayout>

这是我的可绘制对象:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"
    >
    <solid android:color="#FFFFFF"/>
    <stroke android:width="1dp" android:color="#9e9e9e"/>


</shape>

尝试像这样将您的复选框添加到另一个布局中

<LinearLayout
    android:background="@drawable/shp_circle"
    android:layout_width="wrap_content"
    android:layout_height="65dp"
    android:gravity="center">

        <CheckBox
            android:id="@+id/cb_1"
            android:layout_width="wrap_content"
            android:layout_height="65dp"
            android:checked="false" />

</LinearLayout>

我已经按照我的方式更改了您的代码,因此只需将其添加到您的代码中即可。它解决了你的问题。

谢谢