java为什么设置为左/右的布局重力在水平线性布局中没有影响?
我试图让两个TextView
一个在父对象的左侧,另一个在右侧
我原以为layout_gravity
就足够了
但它不起作用。示例代码:
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:orientation="vertical"
安卓:layout_width="300dp"
安卓:layout_height="match_parent">
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="horizontal"
>
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="LEFT"
安卓:textSize="14sp"
安卓:layout_gravity="left"
/>
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="RIGHT"
安卓:textSize="14sp"
安卓:layout_gravity="right"
/>
</LinearLayout>
</LinearLayout>
我做错了什么
# 1 楼答案
LinearLayout
不使用layout_gravity
作为其布局的方向。您可以在水平LinearLayout
中使用layout_gravity
来控制它们垂直放置的方式:顶部、中心或底部你可以把
LinearLayout
改为垂直,然后你的左/右引力会起作用,但是第二个TextView
会比第一个放得更低。我想这不是你想要的在我看来,你似乎对
LinearLayout
提供的布局逻辑不感兴趣(这是一个接一个地放置它的子对象)。你只需要一个左对齐,一个右对齐。如果你把TextView
放在一个FrameLayout
里,你可以用重力来达到这个效果# 2 楼答案
试试这个:
# 3 楼答案
试着这样做: 编辑Vincible的答案