Android 居中 TableLayout

Android Center a TableLayout

我有以下 xml 并且我正在尝试将 TableRow 居中。 我尝试了 strech="*" 但它无法正常工作,因为当我更改名称的长度时两行的宽度都会改变。

有什么想法吗? 谢谢

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:shrinkColumns="1"
        app:visibleGone="@{showData}"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/onShow"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <TextView
                android:id="@+id/header"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:fontFamily="sans-serif"
                android:padding="4dp"
                android:text="Details about:"
                android:textAlignment="textEnd"
                android:textSize="@dimen/employee_text_size_header"
                android:textStyle="bold"
                android:typeface="normal" />

            <TextView
                android:id="@+id/header_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:fontFamily="sans-serif"
                android:maxLines="1"
                android:padding="4dp"
                android:text='Yasir Carlos'
                android:textSize="@dimen/employee_text_size_header"
                android:typeface="normal" />

        </TableRow>
  </TableLayout>

尝试

<TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:shrinkColumns="1"
        app:visibleGone="@{showData}"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toEndOf="@+id/onShow"
        app:layout_constraintTop_toTopOf="parent"
        >
    <TableRow
    android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="header"
        android:gravity="center"/>
</TableRow>

这会起作用fine.I我自己测试了代码。

使用此代码编辑您的布局文件,

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableLayout
    android:id="@+id/tableLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:shrinkColumns="1"
    android:visibleGone="@{showData}"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toEndOf="@+id/onShow"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    tools:ignore="UnknownId">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <TextView
            android:id="@+id/header"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:fontFamily="sans-serif"
            android:padding="4dp"
            android:text="Details about:"
            android:textSize="@dimen/employee_text_size_header"
            android:textStyle="bold"
            android:typeface="normal"
            android:gravity="center"
            android:layout_weight="1"/>

        <TextView
            android:id="@+id/header_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:fontFamily="sans-serif"
            android:maxLines="1"
            android:padding="4dp"
            android:gravity="center"
            android:text='Yasir Carlos'
            android:textSize="@dimen/employee_text_size_header"
            android:typeface="normal"
            android:layout_weight="1"/>

    </TableRow>
</TableLayout>
</android.support.constraint.ConstraintLayout>