Android 布局右对齐
我有以下布局
<?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="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="13">
<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1">
<ImageButton android:background="@null" android:id="@+id/back" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/back" android:padding="10dip" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1">
<ImageButton android:background="@null" android:id="@+id/forward" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/forward" android:padding="10dip" />
</LinearLayout>
</LinearLayout>
<RelativeLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" >
<ImageButton android:background="@null" android:id="@+id/special" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/barcode" android:padding="10dip" android:layout_gravity="right"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
出于这个问题的目的,我只关心布局的下半部分。现在它包含 3 个图像按钮。前 2 个,我希望彼此靠左对齐。第三个,我想与右侧对齐。
现在,前两个按钮是我想要的位置,但第三个按钮顽固地保持左对齐。我该如何使其正确对齐。
I have the following layout in place
<?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="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="13">
<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1">
<ImageButton android:background="@null" android:id="@+id/back" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/back" android:padding="10dip" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1">
<ImageButton android:background="@null" android:id="@+id/forward" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/forward" android:padding="10dip" />
</LinearLayout>
</LinearLayout>
<RelativeLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" >
<ImageButton android:background="@null" android:id="@+id/special" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/barcode" android:padding="10dip" android:layout_gravity="right"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
For the purpose of this question, I am only concerned about the lower half of the layout. Right now it contains 3 imagebuttons. The first 2, I want right next to each other left aligned. The third one, I want to be aligned to the right side.
As is, the first 2 buttons are where I want them to be, but the 3rd is stubbernly staying left-aligned. How would I make it right aligned.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
布局极其低效且臃肿。您不需要那么多的 LinearLayout。事实上,您根本不需要任何
LinearLayout
。仅使用一个
RelativeLayout
。像这样。The layout is extremely inefficient and bloated. You don't need that many
LinearLayout
s. In fact you don't need anyLinearLayout
at all.Use only one
RelativeLayout
. Like this.您只需使用一个
RelativeLayout
即可完成所有这些操作(顺便说一句,它不需要android:orientation
参数)。因此,您可以执行以下操作,而不是使用包含一堆内容的LinearLayout
:正如您所注意到的,缺少一些 XML 参数。我只是展示了您必须输入的基本参数。您可以完成其余的工作。
You can do all that by using just one
RelativeLayout
(which, btw, don't needandroid:orientation
parameter). So, instead of having aLinearLayout
, containing a bunch of stuff, you can do something like:As you noticed, there are some XML parameters missing. I was just showing the basic parameters you had to put. You can complete the rest.
如果你想使用 LinearLayout,你可以使用 < 进行对齐code>layout_weight 与
Space
元素。例如,以下布局将
textView
和textView2
彼此相邻,并且textView3
将右对齐,您可以在没有
Space 的情况下实现相同的效果
如果您将layout_weight
设置为textView2
。只是我喜欢更独立的东西,再加上演示Space
元素。请注意,您应该(但不是必须)设置 < code>layout_width 明确,因为无论如何它都会根据它的重量重新计算(与您应该在垂直
LinearLayout
的元素中设置高度相同的方式)。有关其他布局性能技巧,请参阅 Android 布局技巧系列。If you want to use LinearLayout, you can do alignment with
layout_weight
withSpace
element.E.g. following layout places
textView
andtextView2
next to each other andtextView3
will be right-alignedyou can achieve the same effect without
Space
if you would setlayout_weight
totextView2
. It's just that I like things more separated, plus to demonstrateSpace
element.Note that you should (not must though) set
layout_width
explicitly as it will be recalculated according to it's weight anyway (same way you should set height in elements of verticalLinearLayout
). For other layout performance tips see Android Layout Tricks series.这是 RelativeLayout 的示例:
使用另一种布局(例如 LinearLayout),您只需将 RelativeLayout 更改为 LinearLayout。
This is an example for a RelativeLayout:
With another kind of layout (example LinearLayout) you just simply has to change RelativeLayout for LinearLayout.
为了支持旧版本,可以将 Space 替换为 View,如下所示。在最左边的组件之后和最右边的组件之前添加此视图。此视图的权重=1 将拉伸并填充空间
此处给出了完整的示例代码。它有 4 个组件。右侧和左侧将有两个箭头。文本和微调器将位于中间。
To support older version Space can be replaced with View as below. Add this view between after left most component and before right most component. This view with weight=1 will stretch and fill the space
Complete sample code is given here. It has has 4 components. Two arrows will be on the right and left side. The Text and Spinner will be in the middle.