[英]Buttons in Tablelayout not getting centered
我在Linearlayout中有一個Tablelayout。 在我的表格布局中,我有一些按鈕。 我希望包括按鈕在內的所有內容都居中,因此我添加了行android:gravity="center_horizontal"
不幸的是按鈕不會居中,我也不知道為什么會這樣。
我的代碼是:
<?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>
在<TableRow>
標記中添加android:gravity="center"
並將寬度設置為match_parent
。
由於您使用了wrap_content
的width
,因此沒有多余的空間可以使視圖居中。
希望這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.