2014-10-07 66 views
0

我在布局中有3个按钮。我想要做的是增加按钮的大小,以便填满整个屏幕。我怎么能在XML文件中做到这一点?Android:如何更改在3个相同的按钮划分屏幕

+0

你尝试过什么吗? – Rustam 2014-10-07 18:21:51

+0

显示您想要的一些示例UI。 – Rustam 2014-10-07 18:23:54

+0

我想也许使按钮的高度取决于屏幕上,即。 android:layout_height =“(屏幕尺寸/ 3)”你知道我的意思是 – freeman292 2014-10-07 18:28:22

回答

4
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="3"> 

<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="New Button" 
    android:id="@+id/button" 
    android:layout_weight="1"/> 

<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="New Button" 
    android:id="@+id/button2" 
    android:layout_weight="1"/> 

<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="New Button" 
    android:id="@+id/button3" 
    android:layout_weight="1"/> 
</LinearLayout> 

您需要weightSum

+0

按钮应该是垂直的,如果你使用'weight',那么你应该给'height = 0dip',如果vertical和width = 0dip是水平的。否则'重量'没有影响。 – Rustam 2014-10-07 18:38:28

+0

严厉地说,你不需要'weightSum'。您只需通过分配权重即可达到相同的效果,而无需分配权重总和。我的猜测是,Android可以即时计算重量总和。 – 2014-10-08 06:21:49

0

您可以使用LinearLayoutweight属性。我会猜测并假设你想要垂直布局中的按钮。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Button 
     android:id="@+id/button_1" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 

    <Button 
     android:id="@+id/button_2" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 

    <Button 
     android:id="@+id/button_3" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 

</LinearLayout> 

Buttons将始终填满屏幕,您可以与权重值发挥到修改他们的高度

0

你需要把按钮一的LinearLayout内,使用重量:

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:weightSum="3"> 
<Button 
    android:layout_height="0dip" 
    android:layout_width="wrap_content" 
    android:layout_weight="1" 
    /> 
<Button 
    android:layout_height="0dip" 
    android:layout_width="wrap_content" 
    android:layout_weight="1" 
    /> 
<Button 
    android:layout_height="0dip" 
    android:layout_width="wrap_content" 
    android:layout_weight="1" 
    /> 
+0

他们想垂直按钮。 – Rustam 2014-10-07 18:32:12

+0

这或Carlos J的回答比选定的答案更正确。 – 2014-10-08 06:23:47