简体   繁体   English

Tablelayout中的按钮未居中

[英]Buttons in Tablelayout not getting centered

I have a Tablelayout within a Linearlayout. 我在Linearlayout中有一个Tablelayout。 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" 我希望包括按钮在内的所有内容都居中,因此我添加了行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 . <TableRow>标记中添加android:gravity="center"并将宽度设置为match_parent

As you have used the width to wrap_content there is no extra space to which the view will be centred. 由于您使用了wrap_contentwidth ,因此没有多余的空间可以使视图居中。

Hope this will help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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