在没有静态宽度的任何手机上水平安装按钮

时间:2012-07-18 17:37:30

标签: android xml user-interface

好的,这是一个多年来一直存在问题的问题。我希望在Android屏幕的顶部放置5个水平线按钮。任何手机上的这些按钮都必须是屏幕宽度的20%(以便它们彼此并排放置,没有间隙)。

我无法弄清楚XML。我怎样才能做到这一点?有没有人有任何想法/样品?谢谢!

2 个答案:

答案 0 :(得分:4)

对于按钮行,使用水平线性布局,屏幕的整个宽度,将所有按钮放入其中,并使按钮的布局宽度为0,布局权重等于> 0:

<LinearLayout
     . . .
     android:layout_width="match_parent"
     android:orientation="horizontal"
     />

     <Button
         . . .
         android:layout_width="0"
         android:layout_weight="1"
         />

     <Button
         . . .
         android:layout_width="0"
         android:layout_weight="1"
         />

     . . .
</LinearLayout>

答案 1 :(得分:3)

<LinearLayout
    ...
    layout_width = "match_parent"
    layout_height = "wrap_content" />

    <Button
        ....
        layout_width = "0dp"
        layout_height = "wrap_content"
        layout_weight = 1 />

    <Button
        ....
        layout_width = "0dp"
        layout_height = "wrap_content"
        layout_weight = 1 />

    <Button
        ....
        layout_width = "0dp"
        layout_height = "wrap_content"
        layout_weight = 1 />

    <Button
        ....
        layout_width = "0dp"
        layout_height = "wrap_content"
        layout_weight = 1 />

    <Button
        ....
        layout_width = "0dp"
        layout_height = "wrap_content"
        layout_weight = 1 />

</LinearLayout>