Android:在布局中正确定位按钮
我的布局如下:Android:在布局中正确定位按钮,android,android-layout,Android,Android Layout,我的布局如下: <LinearLayout //container, should adjust height based on CONTENT view height android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"
<LinearLayout //container, should adjust height based on CONTENT view height
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:clickable="false"
android:padding="20dp">
<RelativeLayout //this is the CONTENT view height
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="5">....</RelativeLayout>
...
<RelativeLayout //this is the button layout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2">
<Button android:layout_width="40sp" android:layout_height="40sp"/>
<Button android:layout_width="40sp" android:layout_height="40sp"/>
</RelativeLayout>
</LinearLayout>
....
...
我希望调整容器的高度(LinearLayout
)以包含RelativeLayout
中的所有视图(如左图所示,我们称之为容器)
然后,RelativeLayout
中有两个按钮(显示在右侧)。我需要将它们对应地对齐在RelativeLayot
的上下边框上。真正重要的是,按钮容器的高度应该与容器的高度相同(应该对应)
问题是,如果我尝试为按钮使用android:layout\u alignParentBottom=“true”
和android:layout\u alignParentTop=“true”
属性,它们将拉伸容器高度,并占据整个屏幕高度
那么,我应该用什么魔法来变戏法呢?:) 用这个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:clickable="false"
android:padding="20dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="5"></RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</LinearLayout>
尝试将右侧相对布局的顶部和底部与左侧对齐。 试着这样做:
<RelativeLayout //container, should adjust height based on CONTENT view height
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:clickable="false"
android:padding="20dp">
<RelativeLayout //this is the CONTENT view height
android:id="@+id/contentRL"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="5"
android:layout_alignParentLeft="true">....</RelativeLayout>
...
<RelativeLayout //this is the button layout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_alignTop="@id/contentRL"
android:layout_alignBottom="@id/contentRL"
android:layout_alignParentRight="true">
<Button android:layout_width="40sp"
android:layout_height="40sp"
android:layout_alignParentTop="true"/>
<Button android:layout_width="40sp"
android:layout_height="40sp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
....
...
好的,根据Damien R提供的提示。我通过以下操作成功完成了任务:
- 使用
作为带有参数的根RelativeLayout
,layout\u width=“wrap\u content”
layout\u height=“wrap\u content”
- 使用
作为容器LinearLayout
s的“包装器”。这是因为我需要使用RelativeLayout
属性来布置这些容器李>layout\u weight
RelativeLayout
应为布局高度
。无需在fill\u parent
属性中使用RelativeLayout
和android:layout\u alignBottom=“@id/…”
。只有当android:layout\u alignBottom=“@id/…”
是另一个RelativeLayout
的子视图RelativeLayout
时,这才有效,但事实并非如此,因为我需要使用
的权重LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:clickable="false"
android:padding="10dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/ticketbackground"
android:id="@+id/ticket_layout"
>
<RelativeLayout
android:id="@+id/contentRL"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:layout_alignParentLeft="true">
</RelativeLayout>
<!--second column-->
<RelativeLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="3">
...
</RelativeLayout>
<!--third column with buttons-->
<RelativeLayout
android:id="@+id/sdfsdf"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2">
<Button...
android:layout_alignParentTop="true" />
<Button...
android:layout_alignParentBottom="true" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
...
No,这是不需要的,因为这将拉伸根线性布局hmm,RelativeLayout
似乎没有这样的(或类似的属性)。还有其他方法吗?我已经更新了我的答案,请再次检查并让我知道。(将主布局更改为“相对”并调整子视图相对位置)。这将不起作用-具有布局权重的视图应具有父线性布局。但是根据你的建议,我找到了做我需要做的事情的方法。