Android 工作室利润影响其他项目

Android studio margin effects other items

我尝试将文本视图放在按钮的顶部,并且此按钮居中。如果我向 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>

如果不需要使用 RelativeLayout,我建议使用 ConstraintLayout。 首先,您可以为 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 用于定义文本的大小。

此外,LinearLayoutTableRow 似乎都是多余的,您可能可以直接使用 ButtonTextView