无法将视图右对齐

Can't align view to the right

我正在尝试在父布局的左侧和右侧显示一个视图,如下所示:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="left"
        android:layout_gravity="left" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="right"
        android:layout_gravity="right" />
</LinearLayout>

但结果我看到两个 textView 都只左对齐。我不明白为什么。我设法设置了垂直 layout_gravity 而不是水平的。此外,我可以添加:

android:gravity="right"

到父布局,然后在右侧看到两个视图,但找不到在左侧和右侧显示一个视图的方法。
请帮忙

android:layout_weight="1" 设置为您的两个 TextView

改用相对布局

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="left" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="right" />

</RelativeLayout>

试试这个:

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

        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left"
            android:text="left" />

        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"
            android:text="right" />
    </LinearLayout>

或尝试以下方法

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="left" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="right" />

</RelativeLayout>

编辑: 在方法 1 中,使用权重将整个屏幕分成两部分,并将第一项的重力设置为左侧,将第二项的重力设置为右侧。 我没有将 android:layout_gravity 用于 TextView 。使用 android:gravity。更好的做法是使用 relativeLayout。勾选第二个选项

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="100"
    android:orientation="horizontal">

    <TextView
        android:layout_weight="50"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="left"
       android:gravity="left" />

    <TextView
        android:layout_weight="50"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="right"
        android:gravity="right" />
</LinearLayout>

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="left" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="left" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="right" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="right" />
</LinearLayout>

</LinearLayout>

使用 LinearLayout 的最简单解决方案是使用 gravity="right"

修改正确的文本视图
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="right"
    android:gravity="right" />

但我强烈建议按照@Sjd 的建议使用 RelativeLayout

你也可以试试这个

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

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="left"
        android:text="left" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="right"
        android:text="right" />

</LinearLayout>

这里还有一个技巧;

TextView 之间放置一个不可见的 View。并且它将占据整个剩余区域 android:layout_weight="1",因此 TextViews 将保留在两端。不需要重力。试试下面的代码:)

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="left" />

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="right" />
</LinearLayout>