问题描述
我想并排放置两个 TextView,我希望一个触摸屏幕右侧,另一个触摸左侧.我不想使用数字来定义宽度,因为不同尺寸的屏幕会有不同的表现.所以我正在尝试使用 layout_gravity,但由于某种原因它不起作用.
I'm trying to have two TextViews side-by-side, and I want one to be touching the right-side of the screen and the other, the left-side. I don't want to define the widths using numbers because screens of different sizes would behave differently. So I'm trying to use layout_gravity, which is not working for some reason.
<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>
谁能告诉我为什么?谢谢!
Can anyone tell me why? Thanks!
推荐答案
您可以为每个 TextView
创建一个 LinearLayout
如下:
You can create one LinearLayout
for each TextView
as follows :
<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"
,然后它会工作:)
And the important thing is that in your first LinearLayout
you put android:gravity="start"
and in your second one android:gravity="end"
, then it will work :)
使用 end
而不是 right
来确保在从右到左的语言环境中的正确行为.
Use end
instead of right
to ensure correct behavior in right-to-left locales.
为什么结束"比正确"好?
使用 Gravity#LEFT
和 Gravity#RIGHT
会导致在文本从 right
流向 .使用 Gravity#START
和 Gravity#END
代替.同样,在 XML
重力和 layout_gravity
属性中,使用 start
而不是 left
.对于 paddingLeft
和 layout_marginLeft
等 XML 属性,请使用 paddingStart
和 layout_marginStart
.注意:如果您的 minSdkVersion
小于 17,您应该添加旧的 left/right
属性以及新的 start/right
属性.在不支持 RTL
且 start/right
属性未知并因此被忽略的旧平台上,您需要旧的 left/right
属性.有一个单独的 lint 检查可以捕获该类型的错误.
Using Gravity#LEFT
and Gravity#RIGHT
can lead to problems when a layout is rendered in locales where text flows from right
to left
.Use Gravity#START
and Gravity#END
instead. Similarly, in XML
gravity and layout_gravity
attributes, use start
rather than left
.For XML attributes such as paddingLeft
and layout_marginLeft
, use paddingStart
and layout_marginStart
.NOTE: If your minSdkVersion
is less than 17, you should add both the older left/right
attributes as well as the new start/right
attributes. On older platforms, where RTL
is not supported and the start/right
attributes are unknown and therefore ignored, you need the older left/right
attributes. There is a separate lint check which catches that type of error.
(注意:对于 Gravity#LEFT
和 Gravity#START
,即使针对旧平台,您也可以使用这些常量,因为起始位掩码是左的超集位掩码.因此,您可以使用 gravity="start"
而不是 gravity="left|start"
.)
(Note: For Gravity#LEFT
and Gravity#START
, you can use these constants even when targeting older platforms, because the start bitmask is a superset of the left bitmask. Therefore, you can use gravity="start"
rather than gravity="left|start"
.)
这篇关于两个并排的 TextViews 的重力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!