android:layout_gravity 不按预期工作

11

我有一个简单的LinearLayout,其中包含一个TextView和一个ImageView。我希望TextView中的文本右对齐,但结果显示文本是左对齐的。我的布局XML有什么问题吗?谢谢。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:background="#E8E3E4">
    <LinearLayout android:orientation="horizontal"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        >
        <TextView android:layout_width="260dp" android:layout_height="wrap_content"
            android:text="More Comments" android:textStyle="bold" android:background="#ff0000"
            android:textColor="#121222" android:layout_gravity="right" />
        <ImageView android:layout_width="50dp"
            android:layout_height="wrap_content" android:src="@drawable/arrow_right"
            android:layout_gravity="center" android:scaleType="centerInside" />
    </LinearLayout>
</LinearLayout>

谢谢你,Michael。但是你提到的解决方法也不起作用。

我尝试使用RelativeLayout,但仍然得到了这样的结果: More Comments (图标)

我期望的是 More Comments (图标)

下面是RelativeLayout的XML代码。它有什么问题吗?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TextView android:id="@+id/label" android:layout_width="260dp"
        android:layout_height="wrap_content" android:text="More Comments"
        android:textStyle="bold" android:background="#ff0000"
        android:textColor="#121222" android:layout_gravity="right" />
    <ImageView android:layout_width="50dp" android:layout_height="wrap_content"
        android:src="@drawable/arrow_right" android:layout_gravity="center"
        android:scaleType="centerInside" android:layout_toRightOf="@id/label" />
</RelativeLayout>

根据Michael评论中的解决方案,我得到了下面的XML。 我期望的输出结果是:

     RightLeft

但实际输出结果是:

Right             Left

那么这个解决方法对我没有用。你有什么想法吗?

   <LinearLayout android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
           <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_gravity="right"
                   android:layout_weight="1.0"                       
                   android:text="RIGHT"/>
           <TextView
                   android:layout_width="wrap_content"

                   android:layout_height="wrap_content"
                   android:layout_gravity="left"
                   android:text="LEFT"/>
   </LinearLayout>


他确实做了,只是没有格式化。 - David Thomas
(同时:已编辑为可见,并且作为代码可见...) - David Thomas
顺便问一下,如何正确地格式化代码以便在此处发布代码或XML? - user256239
1
@KKnight,对于内联代码块,最简单的方法是在代码(例如<imageView>)两侧加上反引号。对于代码块,您可以选择代码块并在编写答案/问题时点击“1010”按钮,或将每行代码缩进四个空格。 - David Thomas
4个回答

35

布局方向为水平的LinearLayout不支持右/左对齐。你可以在这里找到一个解决方法。


+1,我在看到这个答案之前花了一些时间尝试找到这些信息。 - Rudi Kershaw
链接并不像人们想象的那样永恒。现在,“这里”指的是“禁止内容警告”。 - EmpathicSage
看起来该组中的所有主题都已被删除,而网络档案馆也没有对其进行抓取。时间会吞噬一切。您可能可以从原始帖子中获取一些线索(显示解决方法的非工作实现)。 - Michael Krauklis

8
我发现了我的xml文件中的问题。在TextView中,我应该使用gravity而不是layout_gravity来将文本右对齐。以下XML代码可以达到预期效果。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TextView android:id="@+id/label" android:layout_width="260dp"
        android:layout_height="wrap_content" android:text="More Comments"
        android:textStyle="bold" android:background="#ff0000"
        android:textColor="#121222" android:gravity="right" />
    <ImageView android:layout_width="50dp" android:layout_height="wrap_content"
        android:src="@drawable/arrow_right" android:layout_gravity="center"
        android:scaleType="centerInside" android:layout_toRightOf="@id/label" />
</RelativeLayout>

那么您应该将此答案标记为“已接受”(单击问题左侧的勾号)。 - David Thomas

1
我只做过一些安卓项目,但 android:layout_gravity 指定了 View 在父元素中的对齐方式,android:gravity 则指定了包含在你所指定的 View 中的 Views 的对齐方式。确保您已经指定了正确的属性。文档没有区分这两者。
如果您发布一些示例 XML,则更容易帮助您。
编辑:
我看到您现在已经发布了 XML。看起来您已经设置了正确的属性,但是如果您要将文本对齐到右侧,为什么要将其放在线性布局中的第一个位置?我建议将其改为从左到右。

0

这是我为我的项目编写的一些代码。它可以实现你想要的功能。只需更改控件类型等内容,你就可以开始运行了。

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:background="#ffdddddd">
    <TextView android:layout_width="260dp" 
        android:layout_height="wrap_content"
        android:text="More Comments" 
        android:textStyle="bold" 
        android:background="#ff0000"
        android:textColor="#121222" 
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true" />
    <ImageView android:layout_width="50dp"
        android:layout_height="wrap_content" 
        android:src="@drawable/arrow_right"
        android:scaleType="centerInside"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />
</RelativeLayout>

嗨,Jared,我尝试了你的代码,将ToogleButton替换为TextView。但是它没有起作用:TextView中的文本没有对齐到右侧。 - user256239

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接