【问题标题】:Gravity of two side-by-side TextViews两个并排的 TextView 的重力
【发布时间】:2016-01-27 13:40:59
【问题描述】:

我正在尝试并排放置两个 TextView,我希望一个触摸屏幕右侧,另一个触摸左侧。我不想使用数字定义宽度,因为不同尺寸的屏幕会表现不同。所以我正在尝试使用 layout_gravity,但由于某种原因它不起作用。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16dp"
    android:layout_gravity="left"
    android:text="rrr"
    android:textColor="@color/secondTextColor"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:textSize="16dp"
    android:text="sss"
    android:textColor="@color/secondTextColor" />

</LinearLayout>

谁能告诉我为什么?谢谢!

【问题讨论】:

  • 这两个文本视图之间是否应该相互接触?还是 1 只接触左边,另一个右边,它们之间不接触?如果他们介于两者之间,那么weight 就足够了,你不需要weightSum

标签: android xml


【解决方案1】:

您可以为每个TextView 创建一个LinearLayout,如下所示:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="start">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:text="rrr"
        android:textColor="#f2f2"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="end">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16dp"
            android:text="sss"
            android:textColor="#f3f3" />
    </LinearLayout>
</LinearLayout>

重要的是,在你的第一个 LinearLayout 中输入 android:gravity="start" 并在第二个 android:gravity="end" 中,然后它会起作用:)

使用end 而不是right 以确保在从右到左的语言环境中行为正确。

为什么“结束”比“正确”更好?

在文本从right 流向left 的语言环境中呈现布局时,使用Gravity#LEFTGravity#RIGHT 可能会导致问题。 请改用Gravity#STARTGravity#END。同样,在XML 重力和layout_gravity 属性中,使用start 而不是left。 对于paddingLeftlayout_marginLeft 等XML 属性,请使用paddingStartlayout_marginStart。 注意:如果您的 minSdkVersion 小于 17,您应该添加旧的 left/right 属性以及新的 start/right 属性。在不支持 RTLstart/right 属性未知并因此被忽略的较旧平台上,您需要较旧的 left/right 属性。有一个单独的 lint 检查可以捕获该类型的错误。

(注意:对于Gravity#LEFTGravity#START,即使针对较旧的平台,您也可以使用这些常量,因为起始位掩码是左侧位掩码的超集。因此,您可以使用gravity="start" 而不是@987654353 @.)

【讨论】:

  • 谢谢!不过,它在没有另外 2 个线性布局的情况下也能正常工作。为什么“结束”比“正确”更好?
  • @JoãoMarcos 如果它对您有帮助,请随时将此答案标记为正确(绿色复选)并点击向上箭头进行投票:P
  • @JoãoMarcos 之所以更好,基本上是为了在不改变布局的情况下轻松支持 RTL 语言(从右到左),请参阅:developer.android.com/training/basics/supporting-devices/…
【解决方案2】:

您可以尝试使用 android:layout_weightandroid:gravity

阅读What does android:layout_weight mean & Layout Weight

 <LinearLayout 
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="horizontal"
   android:weightSum="1" >

 <TextView

    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="left"
    android:text="Intellij" />

 <TextView

    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="right" // You can add end instead of right
    android:text="Amiya" />

 </LinearLayout>

【讨论】:

  • 使用 end 而不是 right 以确保在从右到左的语言环境中的正确行为,left 也是如此
  • @Skizo 好的好的。更新了。谢谢。
  • 我不知道 weightSum 属性。谢谢!
【解决方案3】:

您可以在TextView's 上使用android:layout_weight="1",在width 上使用0dp

【讨论】:

  • 你为什么认为它不优雅?这是一个教程,很好地解释了如何在布局文件中使用权重:link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-01
  • 2013-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多