简体   繁体   中英

Buttons in Tablelayout not getting centered

I have a Tablelayout within a Linearlayout. Inside my Tablelayout I have some buttons. I want that every content including the buttons are centered so I added the line android:gravity="center_horizontal"

Unfortunately the buttons won't get centered and I could not figure why that is.

屏幕

My code is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:padding="10dp"
android:gravity="center_horizontal"
tools:context="com.example.standard.quizzza.MainActivity">

<TextView
    android:id="@+id/frage"
    android:layout_width="wrap_content"
    android:layout_height="60sp"
    android:layout_marginBottom="5dp"
    android:text="Hello World!"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="110dp"
    android:gravvity="center">

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

        <Button
            android:id="@+id/antwortA"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="Antwort A" />

        <Button
            android:id="@+id/antwortB"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="Antwort B" />
    </TableRow>

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

        <Button
            android:id="@+id/antwortC"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="Antwort C" />

        <Button
            android:id="@+id/antwortD"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="Antwort D" />
    </TableRow>

</TableLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="136dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/progressBar"
        android:background="@android:color/transparent"
        android:gravity="center"
        android:text="Frage 1 von 8" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="8"
        android:progress="1" />


</LinearLayout>
</LinearLayout>

Add android:gravity="center" in <TableRow> tag and make the width to match_parent .

As you have used the width to wrap_content there is no extra space to which the view will be centred.

Hope this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM