如何将视图右对齐和放置在LinearLayout的底部

10
我想将一个文本视图(upText)左对齐,并将另一个文本视图(downText)和一个图像视图(image)同时右对齐放在同一行上。
我该如何实现?我尝试过了,但是'文本视图'和图像视图都是左对齐的。
        <TextView
            android:id="@+id/uptext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="left"/>
        <TextView
            android:id="@+id/downtext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:gravity="right|bottom"/>
        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right|bottom"/>
    </LinearLayout>
3个回答

24
不要使用LinearLayout,改用RelativeLayout,并按照以下方式设置:
  • 将第一个TextView设置为android:layout_alignParentLeft="true"
  • 将第二个TextView设置为android:layout_alignParentBottom="true"android:layout_alignParentRight="true"
  • 对于您的ImageView,也需要类似的设置,目前看起来它会重叠在第二个TextView上面。
请注意保留原有的HTML标签。

谢谢。我该如何使第二个TextView和ImageView在父LinearLayout的底部并右对齐布局? - michael
1
位于右下角的那个控件(“Widget A”)应该具有android:layout_alignParentBottom="true"android:layout_alignParentRight="true"属性。另一个控件(“Widget B”)应该具有android:layout_alignParentBottom="true"android:layout_toLeftOf="..."属性,其中“...”是 Widget A 的 android:id - CommonsWare

0

我知道这篇文章有点老了,但以防万一有人在寻找答案时遇到它;

父线性布局是需要指定重力以使子项与所需行为对齐的地方,这就是为什么上面的帖子解释说线性布局不可能出现两个不同的行为,因为子项无法决定在线性布局中如何对齐自己

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="200dp"
android:layout_height="50dp"
android:gravity="bottom|right">

<TextView
    android:layout_width="40dp"
    android:layout_height="20dp" 
    android:text="test"/></LinearLayout>

还应该指出,父线性布局必须具有定义的大小,而不能是wrap-content,否则这将不起作用,因为wrap-content意味着布局中没有额外的空间用于定位,因此至少需要宽度和高度的'match-parent',以及具有比子线性布局本身的wrap-content更大的父级大小。

希望这可以帮助到您。


0
使用RelativeLayout
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom">

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="hi"
        android:textStyle="bold|italic"
        android:gravity="right"/>
    <TextView
        android:id="@+id/text2"
        android:layout_below="@id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="All"
        android:gravity="right"/>

</RelativeLayout>

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