简体   繁体   English

为什么将layout_gravity设置为left / right在水平LinearLayout中不起作用?

[英]Why does layout_gravity set to left/right have no effect in horizontal LinearLayout?

I am trying to have two TextView s one to the left side of the parent and the other to the right. 我试图有两个TextView的一个在父级的左侧,另一个在右侧。
I was expecting that layout_gravity would be enough to do the trick. 我期望layout_gravity足以解决问题。
But it doesn't work. 但这是行不通的。 Example code: 示例代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="300dp"
              android:layout_height="match_parent">

    <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:text="LEFT"
            android:textSize="14sp"
            android:layout_gravity="left"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RIGHT"
            android:textSize="14sp"
            android:layout_gravity="right"
            />
    </LinearLayout>

</LinearLayout>  

What am I doing wrong? 我究竟做错了什么?

Try this: 尝试这个:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="300dp"
  android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="LEFT"
        android:layout_weight=".5"
        android:layout_gravity="start"
        android:gravity="start"
        android:textSize="14sp" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="RIGHT"
        android:layout_weight=".5"
        android:layout_gravity="end"
        android:gravity="end"
        android:textSize="14sp"/>
</LinearLayout>

LinearLayout doesn't use layout_gravity for the orientation it is laying out. LinearLayout不会将layout_gravity用于其布局的方向。 You could use layout_gravity in a horizontal LinearLayout to control how they are place vertically: top, centre or bottom. 您可以在水平LinearLayout使用layout_gravity来控制垂直放置的方式:顶部,中心或底部。

You could change your LinearLayout to vertical, then your left/right gravities would work, but the second TextView would be placed further down than the first one. 您可以将LinearLayout更改为垂直,然后您的左/右重力将起作用,但是第二个TextView将比第一个TextView I assume this is not what you want. 我认为这不是您想要的。

It seems to me you are not interested in the layout logic that LinearLayout provides (which is placing it's children one after the other). 在我看来,您对LinearLayout提供的布局逻辑不感兴趣(这是将其子项一个接一个地放置)。 You want just one to be left aligned and one right aligned. 您只希望一个左对齐,一个右对齐。 You can use gravity for that effect if you put your TextView s in a FrameLayout instead. 如果将TextView放在FrameLayout ,则可以使用重力来实现这种效果。

Try this way : Editing Eenvincible's Answer... 尝试这种方式:编辑Eenvincible的答案...

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1"
    >
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:text="LEFT"
        android:textSize="14sp"
        android:layout_gravity="left"
        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:text="RIGHT"
        android:textSize="14sp"
        android:layout_gravity="right"
        />
</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM