Android layout_weight不工作

Jas*_*sby 4 android android-layout

我正在制作一个带有5个按钮的片段.当它在像平板电脑这样的大屏幕上时,我会使用一个片段将所有5个按钮放在顶部.我将所有layout_width设置为1,但按钮不会像预期的那样在屏幕上均匀分布.

预期[全部] [我的] [按钮] [转] [这里]

我得到什么b1 b2 b3 b4 b5 b3和b5只是一个按钮...只是显示它打破了一个单词[全部] [我的] [但] [转] [她]并将它放下一行.[吨] [e]

我一直在玩它并尝试不同的东西,但我不能让它改变.任何帮助表示赞赏.谢谢.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/xlarge_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
    android:id="@+id/tip_btn"
    android:layout_width="0px"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:text="@string/tip_button"
    android:onClick="tipButton">
</Button>

<Button
    android:id="@+id/preference"
    android:layout_width="0px"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:text="@string/settings"
    android:onClick="showPreferences">
</Button>

<Button
    android:id="@+id/rate_btn"
    android:layout_width="0px"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:text="@string/rate_button"
    android:onClick="rateButton">
</Button>

<Button
    android:id="@+id/feedback_btn"
    android:layout_width="0px"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:text="@string/feedback_button"
    android:onClick="feedbackButton">
</Button>

<Button
    android:id="@+id/cancel_btn"
    android:layout_width="0px"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:text="@string/exit_button"
    android:onClick="cancelButton">
</Button>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Mar*_*hre 21

简答:将按钮的layout_width设置为0. layout_weight用于确定如何在布局中的元素之间传播剩余空间.因此,如果未将宽度设置为零,则按钮内容的大小将影响它们的大小.

这里有两篇很好的博客文章解释了权重如何工作:

http://blog.stylingandroid.com/archives/297 http://blog.stylingandroid.com/archives/312


Oma*_*nik 7

由于您使用的是layout_weight,因此您应该为layout_width提供0dp.它应该工作.