如何在水平线性布局Android中右对齐小部件?
这是我正在使用的代码,但它不起作用:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView android:text="TextView" android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right">
</TextView>
</LinearLayout>
This is the code I am using and it is not working:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView android:text="TextView" android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right">
</TextView>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
尝试将layout_width更改为
android:layout_width="match_parent"
,因为gravity:"right"
会对齐layout_width内的文本,如果您选择换行内容,则它不会去哪里,但如果你选择匹配父母它可以去右边。Try to change the layout_width to
android:layout_width="match_parent"
becausegravity:"right"
aligns the text inside the layout_width, and if you choose wrap content it does not have where to go, but if you choose match parent it can go to the right.添加视图有点困难,它覆盖了所有屏幕宽度,如下所示:
尝试这段代码:
Adding view is bit difficult and it cover all screen width like this:
Try to this code:
如果是 TextView:
In case with TextView:
要在
LinearLayout
的开头和末尾对齐一个元素,您可以将其包装在RelativeLayout
中。该示例的结果如下:
图像链接
注意:您可以将任何您想要的内容包裹在里面并对齐。
For aligning one element at start and one at the end of the
LinearLayout
, you can wrap it in anRelativeLayout
.The result of this example is following:
Link to the image
Note: You can wrap whatever you want inside and align it.
这是我的 xml,向右对齐的动态组件,在我的情况下,我使用 3 个按钮
,结果
您可以隐藏右侧第一个按钮,更改可见性消失,并且
在可见性消失后,我的代码仍然右侧对齐第一个右侧组件
this is my xml, dynamic component to align right, in my case i use 3 button
and the result
you can hide the right first button with change visibility GONE, and this my code
still align right, after visibility gone first right component
这是一个示例。整理关键如下
完整代码
Here is a sample. the key to arrange is as follows
Complete code
使用 match_parent 和重力将 TextView 文本设置为右侧,如下所示:
Use match_parent and gravity to set the TextView text to right, like this:
试试这个..
Try This..
尝试在水平
LinearLayout
内您想要正确看到的元素之前添加空View
,例如:Try to add empty
View
inside horizontalLinearLayout
before element that you want to see right, e.g.:如果您不希望所有内容都位于右侧,请勿将 LinearLayout 的重力更改为“右侧”。
尝试:
fill_parent
right
代码:
Do not change the gravity of the LinearLayout to "right" if you don't want everything to be to the right.
Try:
fill_parent
right
Code:
作为对 alcsan 答案的补充,您可以从 API 14 (Android 4.0 ICE_CREAM_SANDWICH) 开始使用
Space
,此处文档。对于支持 API 级别低于 14 的应用程序,有一个
android.support.v4 .widget.Space
自 Android 支持库 r22.1.0 起。As a supplement to alcsan's answer, you can use
Space
since API 14 (Android 4.0 ICE_CREAM_SANDWICH), document here.For apps that supporting API levels under 14, there is an
android.support.v4.widget.Space
since Android Support Library r22.1.0.使用线性布局
使用FrameLayout
使用相对布局
With Linear Layout
with FrameLayout
with RelativeLayout
设置视图的
layout_weight="1"
就可以了!setting the view's
layout_weight="1"
would do the trick.!将
android:gravity="right"
添加到 LinearLayout。假设TextView
有layout_width="wrap_content"
Add
android:gravity="right"
to LinearLayout. Assuming theTextView
haslayout_width="wrap_content"
只需在您的 Liner 布局中添加
android:gravity="right"
即可。just add
android:gravity="right"
in your Liner Layout.对于水平方向的 LinearLayout ,将
layout_weight 赋予除要右对齐的视图之外的其他子视图。这非常有效。
For
LinearLayout
with horizontal orientation, givelayout_weight
to other child view except for the view that you want to align right. This works perfectly.我用最简单的方法做到了:
只需使用一个 RelativeLayout 并将您的 子 视图 放入其中,您想要将其放置在 右侧。
希望它能帮助你。
I have done it by easiest way:
Just take one RelativeLayout and put your child view in it, which you want to place at right side.
Hope it will help you.
具有
layout_width="fill_parent"
的线性布局
以及具有相同布局宽度
+右侧重力
的小部件将对齐它在右边。我在下面的示例中使用了 2 个
TextView
,topicTitle
在左侧,topicQuestions
在右侧。输出
linear layout
withlayout_width="fill_parent"
and also the widget with samelayout width
+gravity as right
would align it to the right.I'm using 2
TextView
s in following example,topicTitle
on the left andtopicQuestions
on the right.Output
不需要使用任何额外的视图或元素:
//这就是如此简单
//这是左对齐
//这是右对齐
No need to use any extra view or element:
//that is so easy and simple
//this is left alignment
//this is right alignment
您应该使用relativelayout并拖动它们直到看起来不错:)
You should use a RelativeLayout and just drag them until it looks good :)