我有一个“后退”和一个“主页”按钮,我试图将它们与屏幕右上角对齐。我希望两个按钮大小相同,但仍然是 wrap_content
,这样它们就不会占用额外的空间。下面的布局几乎可以工作,只是主页按钮有点困惑,而且比后退按钮小一点点。
有人知道如何解决这个问题吗?
如果我将第二个 LinearLayout 更改为 fill_parent
,按钮的宽度会变得均匀并且完美对齐,但它们会占用太多空间并变得非常宽。
屏幕截图:
代码:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="horizontal">
<Button
android:id="@+id/home"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Home" />
<Button
android:id="@+id/back"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Back" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
谢谢!
最佳答案
发生这种情况是因为您选择的宽度不足以容纳第一个按钮的文本。然后文本将换行成两行。这反过来会导致默认基线对齐机制将第一个按钮向下移动。要实现您想要的效果,请将属性measureWithLargestChild添加到LinearLayout:
<LinearLayout
android:measureWithLargestChild="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="horizontal">
<Button
android:id="@+id/home"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Home" />
<Button
android:id="@+id/back"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Back" />
</LinearLayout>
关于android - 使 Android 按钮宽度相同时出现奇怪的视觉问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9082700/