将 TextView 彼此对齐 - Android

Align a TextView next to each other - Android

我在布局中有 2 个文本视图。 第一个在左边,第二个在右边 第一个宽度应为 80% 第二个宽度应为 20%

我怎样才能做到?

我不知道选择哪种布局:线性布局还是相对布局

谢谢!

您可以使用权重 属性 80 和 20

的线性布局
  <LinearLayout
                android:orientation="horizontal"
                android:layout_height="40dp"
                android:layout_width="match_parent">
                <TextView
                    android:text="yes"
                    android:layout_width="0dp"
                    android:layout_weight="80"
                    android:layout_height="40dp"
                    android:id="@+id/textViewOne"
                    android:textStyle="bold"
                    android:textSize="17sp"/>
                <TextView
                    android:text="No"
                    android:layout_width="0dp"
                    android:layout_weight="20"
                    android:layout_height="40dp"
                    android:id="@+id/textViewTwo"
                    android:textStyle="bold"
                    android:textSize="17sp" />
            </LinearLayout>

您可以使用 LinearLayout 的 weightSum 属性 和每个子视图的 layout_weight。简单地:

<LinearLayout
            android:orientation="horizontal"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:weight_sum="100">
            <TextView
                android:layout_width="0dp"
                android:layout_weight="80"
                android:layout_height="wrap_content"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="20"
                android:layout_height="wrap_content" />
        </LinearLayout>

使用线性布局。它提供了以 layout_weight 和 weightSum 属性的形式以百分比指定宽度或高度的工具。

<LinearLayout
            android:orientation="horizontal"
            android:weightSum="10"
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
            <TextView
                android:text="first text view"
                android:layout_width="0dp" 
                android:layout_weight="8"
                android:layout_height="wrap_content"
                />
            <TextView
                android:text="second text view"
                android:layout_width="0dp"
                android:layout_weight="2"
                android:layout_height="wrap_content"
                />
        </LinearLayout>

应该可以。这里权重总和为 10 分为权重 8 和 2 ,即 80% 和 20% 。将解决您的问题。