有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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>  

我做错了什么


共 (3) 个答案

  1. # 1 楼答案

    LinearLayout不使用layout_gravity作为其布局的方向。您可以在水平LinearLayout中使用layout_gravity来控制它们垂直放置的方式:顶部、中心或底部

    你可以把LinearLayout改为垂直,然后你的左/右引力会起作用,但是第二个TextView会比第一个放得更低。我想这不是你想要的

    在我看来,你似乎对LinearLayout提供的布局逻辑不感兴趣(这是一个接一个地放置它的子对象)。你只需要一个左对齐,一个右对齐。如果你把TextView放在一个FrameLayout里,你可以用重力来达到这个效果

  2. # 2 楼答案

    试试这个:

    <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>
    

  3. # 3 楼答案

    试着这样做: 编辑Vincible的答案

    <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>