java android:layout_gravity 没有按预期工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2119701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:32:57  来源:igfitidea点击:

android:layout_gravity doesn't work as expected

javaandroid

提问by user256239

I have a simple LinearLayout with one TextView and one ImageView. I want the text in the TextView to be aligned to the right, but the result show that the Text was aligned to the left. Is there anything wrong with my layout xml? Thanks.

我有一个带有一个 TextView 和一个 ImageView 的简单 LinearLayout。我希望 TextView 中的文本右对齐,但结果显示 Text 是左对齐的。我的布局 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>


Thanks, Michael. But the workaround you mentioned doesn't work either.

谢谢,迈克尔。但是您提到的解决方法也不起作用。

I tried to use RelativeLayout, but it still gave me result like: More Comments (Icon)

我尝试使用RelativeLayout,但它仍然给了我这样的结果:更多评论(图标)

What I expected is More Comments (Icon)

我期望的是更多评论(图标)

The RelativeLayout xml is below. Is there anything wrong with it?

相对布局 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>


Following the workaround in Michael's comment, I have the XML below. I expected the output to be:

按照迈克尔评论中的解决方法,我有下面的 XML。我预计输出是:

     RightLeft

but the actual output is:

但实际输出是:

Right             Left

So the workaround doesn't work for me. Any thoughts?

所以解决方法对我不起作用。有什么想法吗?

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

采纳答案by user256239

I figured out what is wrong with my xml. I should have used gravity instead of layout_gravity to right-align the text in TextView. The following XML works as expected.

我发现我的 xml 有什么问题。我应该使用重力而不是 layout_gravity 来右对齐 TextView 中的文本。以下 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>

回答by Michael Krauklis

LinearLayouts with a horizontal orientation don't support right/left gravity. You can find a workaround here.

具有水平方向的 LinearLayouts 不支持右/左重力。您可以在此处找到解决方法。

回答by smelch

I've done only a few projects with android, but android:layout_gravity specifies the alignment for the View within the parent, android:gravity specifies the alignment of the Views contained within the View whose gravity you are specifying. Make sure you have the proper one specified. The documentation does not distinguish between these two.

我只用 android 完成了几个项目,但是 android:layout_gravity 指定了父视图中的对齐方式,android:gravity 指定了包含在您指定重力的视图中的视图的对齐方式。确保您指定了正确的一个。文档没有区分这两者。

It would be easier to help you if you posted some sample XML.

如果您发布了一些示例 XML,则更容易为您提供帮助。

EDIT:

编辑:

I see you have posted the XML now. It does look like you have the proper attribute set, but if you are aligning the text to the right, why would you put it first in the linear layout? I suggest changing it to go left to right.

我看到您现在已经发布了 XML。看起来您确实设置了正确的属性,但是如果您将文本向右对齐,为什么要将其放在线性布局中?我建议将其更改为从左到右。

回答by Jared

Here is some code I wrote for my project. It accomplishes what you're looking for. I'd just change the control types and such and you should be off and running.

这是我为我的项目编写的一些代码。它完成了你正在寻找的东西。我只需更改控件类型等,您就应该关闭并运行。

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