Android 如何在水平 LinearLayout 中将元素向右对齐?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20728415/
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 align an element to right in horizontal LinearLayout?
提问by MRefaat
I have a LinearLayout
that contains a lot of TextViews
and ImageButtons
, I want to align some of these elements to right, i had a look at thisand thisbut i can't use their tips as i can't change the orientation
and can't make android.gravity:right
as i don't want to align all the elements to right, also i can't use nested layouts or but the desired elements into RelativeLayout
because that shifts the rest of elements to the left and i want them at the center.
我有一个LinearLayout
包含很多TextViews
和的ImageButtons
,我想将其中一些元素对齐,我看过这个和这个,但我不能使用他们的提示,因为我不能改变orientation
,也不能android.gravity:right
像我不想将所有元素向右对齐,我也不能使用嵌套布局或所需的元素,RelativeLayout
因为这会将其余元素向左移动,而我希望它们位于中心。
this is my code:
这是我的代码:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:gravity="center"
android:background="@drawable/media_mediabar"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/move_backward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:background="@android:color/transparent"
android:clickable="true"
android:scaleType="centerInside"
android:src="@drawable/media_button_rewind"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:tag="released"/>
<ImageButton
android:id="@+id/rmeote_mines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:background="@android:color/transparent"
android:clickable="true"
android:scaleType="centerInside"
android:src="@drawable/remote_minus" />
<TextView
android:id="@+id/remote_plus_minus"
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp" />
.
.
.<!.. some other elements ..!>
</LinearLayout>
The desired result:
想要的结果:
回答by GareginSargsyan
The simplest solution would be using empty views with weights as separators.
最简单的解决方案是使用带有权重作为分隔符的空视图。
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- Left button -->
<Button ...
... />
<View android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<!-- Middle button -->
<Button ...
... />
<View android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<!-- Right button -->
<Button ...
... />
</LinearLayout>
The separator views can be made invisible as an optimization, because they don't draw anything and are used only for layout. You can tweak the actual 'layout_weight' values to get the desired layout. Starting from API level 14 you can use instances of Spaceas separators which will improve performance and readability (there is also a version of Space in the support library).
作为优化,分隔符视图可以不可见,因为它们不绘制任何内容并且仅用于布局。您可以调整实际的“layout_weight”值以获得所需的布局。从 API 级别 14 开始,您可以使用Space 的实例作为分隔符,这将提高性能和可读性(支持库中还有一个 Space 版本)。
回答by Ridcully
For such a complex layout you'd be way better of using RelativeLayout
instead.
对于如此复杂的布局,您最好使用它RelativeLayout
。
回答by Ilya Gazman
i can't use nested layouts
我不能使用嵌套布局
Then you can't solve your problem.
那你的问题就解决不了了。
Nested layout are the heart of Android layout, to create such complex view that you desire, I think you must use nested layouts.
嵌套布局是 Android 布局的核心,要创建您想要的如此复杂的视图,我认为您必须使用嵌套布局。
@Ridcully suggested you to use RelativeLayout
, this is a good idea. You can combine it with few linear layouts and you be fine.
@Ridcully 建议您使用RelativeLayout
,这是个好主意。你可以将它与一些线性布局结合起来,你会没事的。
I think that RelativeLayout
should be your base layout.
我认为这RelativeLayout
应该是您的基本布局。