Android 如何布局视图右对齐和 LinearLayout 的底部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2664073/
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
How to layout view right aligned and bottom of an LinearLayout
提问by michael
I am trying to layout 1 textview (upText) left aligned and 1 textview (downText) and an image view (image) both on the same line and right aligned.
我正在尝试将 1 个文本视图(upText)左对齐和 1 个文本视图(downText)和一个图像视图(图像)在同一行和右对齐。
how can I do that? I tried that, but both 'textview' and image view at left aligned.
我怎样才能做到这一点?我试过了,但左对齐的“textview”和图像视图。
<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>
回答by CommonsWare
Don't use a LinearLayout
. Use a RelativeLayout
, with
不要使用LinearLayout
. 使用RelativeLayout
, 与
- your first
TextView
set withandroid:layout_alignParentLeft="true"
- your second
TextView
set withandroid:layout_alignParentBottom="true"
andandroid:layout_alignParentRight="true"
- something similar for your
ImageView
, which presently looks like it is going to overlap the secondTextView
- 你的第一
TextView
组android:layout_alignParentLeft="true"
- 你的第二
TextView
组android:layout_alignParentBottom="true"
和android:layout_alignParentRight="true"
- 类似于您的
ImageView
,目前看起来它将与第二个重叠TextView
回答by Corey
I realize this post is a bit old but just in case someone comes across this in their search for clarity;
我意识到这篇文章有点旧,但以防万一有人在寻找清晰度时遇到这个问题;
The parent linear layout is where gravity needs to be specified for the child to align with the desired behavior which is why the above posts are explaining that linear layout is not possible for two separate behaviors to occur since a child cannot decide where to align itself within a linear layout.
父线性布局是需要为孩子指定重力以与所需行为对齐的地方,这就是为什么上述帖子解释线性布局不可能发生两个单独的行为,因为孩子无法决定在何处对齐自己线性布局。
<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>
It should also be said that the parent linear layout must have a defined size and not be wrap-content or this will not work since wrap content implies that there will be no extra space in the layout for positioning, so at least 'match-parent' for width and height is necessary as well as having a parent with a greater size than wrap-content for the child linear layout itself.
还应该说,父线性布局必须具有定义的大小,而不是 wrap-content 否则这将不起作用,因为 wrap content 意味着布局中将没有额外的空间用于定位,所以至少 'match-parent ' 对于宽度和高度来说是必要的,并且对于子线性布局本身来说,父级的大小大于 wrap-content 。
Hope this helps.
希望这可以帮助。
回答by M.S.T
Using 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>