Android Center TableLayout - PullRequest
       7

Android Center TableLayout

0 голосов
/ 12 сентября 2018

У меня есть следующий xml, и я пытаюсь центрировать TableRow. Я попытался strech = "*", но он не работает должным образом, потому что ширина обеих строк изменяется, когда я изменяю длину имени.

Есть идеи? Спасибо

enter image description here

<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>

Ответы [ 2 ]

0 голосов
/ 12 сентября 2018

Это будет работать нормально. Я сам проверил код.

Отредактируйте файл макета с помощью этого кода,

<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>
0 голосов
/ 12 сентября 2018

Попробуйте

<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>
...