【问题标题】:Align a TextView next to each other - Android将 TextView 彼此相邻对齐 - Android
【发布时间】:2023-04-06 13:16:01
【问题描述】:

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

我该怎么做?

我不知道该选择哪种布局:线性布局或相对布局

谢谢!

【问题讨论】:

  • 使用LinearLayout 使用权重来定义查看您所要求的内容

标签: android textview width


【解决方案1】:

您可以使用权重属性为 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>

【讨论】:

    【解决方案2】:

    您可以使用 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>
    

    【讨论】:

      【解决方案3】:

      使用线性布局。它提供了以 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% 。会解决你的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-21
        • 2014-05-18
        • 1970-01-01
        • 2013-04-23
        • 1970-01-01
        相关资源
        最近更新 更多