android - 使 Android 按钮宽度相同时出现奇怪的视觉问题

标签 android android-layout

我有一个“后退”和一个“主页”按钮,我试图将它们与屏幕右上角对齐。我希望两个按钮大小相同,但仍然是 wrap_content,这样它们就不会占用额外的空间。下面的布局几乎可以工作,只是主页按钮有点困惑,而且比后退按钮小一点点。

有人知道如何解决这个问题吗?

如果我将第二个 LinearLayout 更改为 fill_parent,按钮的宽度会变得均匀并且完美对齐,但它们会占用太多空间并变得非常宽。

屏幕截图:

http://dl.dropbox.com/u/24225416/Images/back_home_buttons.jpg

代码:

<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/

相关文章:

android - 在可点击小部件中获取点击事件的问题

java - Android 从 SQLite 数据库填充字符串数组

java - 简单 XML 序列化第三方库

android - 如何以编程方式引用 View ?

android - EditText 填充在 Android 中无法正常工作

android - Google Goggles 通过 IntentIntegrator 工作但没有 'Capture' 按钮

android - 如何通知用户更新现有应用程序?

android - gridview 的样式自定义适配器

android - 如何在按下 EditText 时阻止 Scrollview 将背景图像移动到顶部?

java - 如何在android中创建两个固定大小的 View 和跨越它们之间的一个 View ?