2014-03-28 129 views
0

我有一个线性布局和里面的一个按钮。我希望按钮适合布局宽度的大约90%。以下一些教程中,我使用的属性layout_weight,这就是我的代码:Android的重量和布局宽度

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#FFFFFF" 
    android:weightSum="5" > 

<Button 
    android:id="@+id/newgame" 

    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="4" 

    android:text="@string/newgame" 
    android:textSize="16sp" 
    android:typeface="monospace" 
    android:textStyle="bold" 
    android:background="@drawable/backrepeat" /> 

</LinearLayout> 

现在,我的按钮消失,如果我设置layout_width到0dp像极了教程说...可能是什么问题呢? 在此先感谢。

+1

改变方向的LinearLayout水平或添加重量为1的不可见视图。 – joao2fast4u

+0

即使您使用重量,如果您使用正确的内容,您也只想知道方向t ...方向和价值... –

回答

0

试试这个,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFFFFF" 
> 

<Button 
    android:id="@+id/newgame" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="9" 

    android:text="@string/newgame" 
    android:textSize="16sp" 
    android:typeface="monospace" 
    android:textStyle="bold" 
    android:background="@drawable/backrepeat" /> 

</LinearLayout> 

默认情况下,线性布局的方向是水平的,因此现在将调整它考虑到横向宽度。 :)

+0

谢谢!我只需将android:weightSum =“10”添加到LinearLayout以使其工作:) – esseara

+0

欢迎您。 :)很高兴听到它的工作。 –

2

线性布局权重只在布局方向的维度上分配空间。你有一个垂直线性布局和一个0px宽的元素 - 重量机制不会添加任何东西。将线性布局方向更改为水平方向。

1

Vertical orientation!

Vertical

您可以轻松地设置Weight你只是用这样的.... 相反的0dp必须使用0dip

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#FFFFFF" 
android:orientation="vertical" 
android:weightSum="5" > 

<Button 
    android:id="@+id/newgame" 
    android:layout_width="0dip" 
    android:layout_height="wrap_content" 
    android:layout_weight="4" 
    android:background="@drawable/backrepeat" 
    android:text="@string/newgame" 
    android:textSize="16sp" 
    android:textStyle="bold" 
    android:typeface="monospace" /> 

</LinearLayout>