我嘗試將 textview 放在按鈕的頂部,并且該按鈕居中。如果我向 textview 添加邊距,那么按鈕也會移動。如何修復按鈕并為 textview 添加一些邊距?
這是我的xml檔案
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#fff"
android:orientation="horizontal"
android:weightSum="1" >
>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout android:layout_marginBottom="100sp"
>
<TableRow android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
<TextView
android:text="asdasdasd"
android:textColor="@color/black" >
</TextView>
</TableRow>
</LinearLayout>
<LinearLayout >
<TableRow android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@color/white">
<Button
android:id="@ id/button"
android:layout_width="100sp"
android:layout_height="100sp"
android:layout_weight="1"
android:background="@drawable/round_button"
android:text="Ba?lat"
android:textColor="@color/white" />
</TableRow>
</LinearLayout>
</TableLayout>
</RelativeLayout>
uj5u.com熱心網友回復:
如果不需要使用 RelativeLayout,我建議使用 ConstraintLayout。首先,您可以為 Button 設定所需的位置,然后將文本底部設定為約束到您的 Button。之后,您可以在文本中添加邊距,而不必擔心影響 Button 位置
您的布局可能如下所示:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:layout_gravity="center"
android:background="#fff">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
app:layout_constraintBottom_toTopOf="@ id/linearLayout"
app:layout_constraintEnd_toEndOf="@ id/linearLayout"
app:layout_constraintStart_toStartOf="@ id/linearLayout">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="asdasdasd"
android:textColor="@color/black">
</TextView>
</TableRow>
</LinearLayout>
<LinearLayout
android:id="@ id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white">
<Button
android:id="@ id/button"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
android:background="@drawable/round_button"
android:text="Ba?lat"
android:textColor="@color/white" />
</TableRow>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
PS:超出了問題的主題,但我建議dp用于設定視圖大小,因為sp用于定義文本的大小。
此外,兩者LinearLayout和TableRow似乎都是多余的,您可能可以直接使用ButtonandTextView
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/456582.html
上一篇:我正在制作一個顯示數字表的應用程式,但它只顯示最后一次計算,我想要從1到10的所有計算
下一篇:從textarea中查找分隔符
