Java 安卓:布局和重力不能像预期的那样工作
我有一个简单的线性布局,有一个文本视图和一个图像视图。我希望文本视图中的文本向右对齐,但结果显示文本向左对齐。我的布局xml有什么问题吗?谢谢Java 安卓:布局和重力不能像预期的那样工作,java,android,Java,Android,我有一个简单的线性布局,有一个文本视图和一个图像视图。我希望文本视图中的文本向右对齐,但结果显示文本向左对齐。我的布局xml有什么问题吗?谢谢 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="
<?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>
但实际产出是:
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>
我只使用android完成了几个项目,但是android:layout\u gravity指定父视图中视图的对齐方式,android:gravity指定要指定其重力的视图中包含的视图的对齐方式。确保指定了正确的。文件中没有区分这两种情况 如果您发布一些示例XML,将更容易帮助您 编辑:
我看到您现在已经发布了XML。看起来确实设置了正确的属性,但如果要将文本向右对齐,为什么要将其放在线性布局的第一位?我建议将其改为从左向右。具有水平方向的线性布局不支持右/左重力。您可以找到解决方法。以下是我为项目编写的一些代码。它完成了你想要的。我只要改变控制类型之类的,你就可以关机运行了
<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>
我发现我的xml有什么问题。我应该使用重力而不是布局重力来右对齐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>
他这样做了,只是没有格式化。另外:编辑后代码可见,并且作为代码可见……顺便问一下,为了在此处发布代码或xml,格式化代码的正确方法是什么?@KKnight,对于内联代码块,最简单的方法是在代码示例的两侧加上一个反勾。对于代码块,您可以在编写答案/问题时选择代码块并点击“1010”按钮,也可以将每行代码缩进四个空格。嗨,Jared,我用TextView替换ToogleButton尝试了您的代码。它不起作用:文本视图中的文本没有向右对齐。然后你应该将此答案标记为“已接受”。单击问题左侧的勾号。+1,在看到此答案之前,我花了一些时间试图查找此信息。链接并不像人们想象的那样永恒。这里现在指的是禁止内容警告。看起来该组中的所有主题都被删除了,而wayback机器从未将其删除。时间吞噬一切。您可能会从显示该变通方法的非工作实现的原始帖子中获得一些线索。
<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>
<?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>