【发布时间】:2011-12-03 14:44:39
【问题描述】:
为什么我的相对布局占据了全屏宽度
<?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:background="#f00"
>
<Button
android:id="@+id/Button01"
android:text="Press Here"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:id="@+id/Button02"
android:text="02"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</RelativeLayout>
我已经指定了“wrap_content”的相对布局,那为什么它会占据全屏空间。即使我说 android:layout_width="fill_parent",输出也是一样的。
请赐教!
编辑:我想我之前的问题不是很清楚。对此表示歉意。 当我在相对布局中有 2 个子视图并且其中一个与父视图左对齐,另一个右对齐且相对布局宽度为 WRAP_CONTENT 时,我预计布局宽度只是 2 个按钮宽度的总和(不是这就是 WRAP_CONTENT 的意思??)。我知道还有其他方法可以实现我正在寻找的 UI,但我只是想正确理解这些相关的布局标签。
编辑 2:我进行了一些实验,看起来如果我们使用 Layout_AlighParentRight,其父级宽度为 WRAP_CONTENT,则上部布局宽度用于计算(就像下面指出的几个答案)。但是我们只使用了 Layout_alignParentLeft 然后它按预期工作并且布局宽度没有扩展到整个屏幕。感谢大家的帮助!
【问题讨论】:
标签: android android-relativelayout