Reputation: 880
I have a Linear Layout and a button inside it. I want the button to fit about 90% of the layout width. Following some tutorial I used the attribute layout_weight and that's my code:
<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>
Now, my button disappear if I set the layout_width to 0dp like most of the tutorials say... What could be the problem? Thanks in advance.
Upvotes: 0
Views: 170
Reputation: 1442
You can set the Weight easily you just use like this....Instead of 0dp you must use the 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>
Upvotes: 1
Reputation: 6792
Try this,
<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>
By default the orientation of Linear Layout is horizontal so it will now align it considering the horizontal width. :)
Upvotes: 0
Reputation: 152787
Linear layout weight distributes space only in the dimension of the layout's orientation. You have a vertical linear layout and an element that is 0px wide - the weight mechanism won't add anything to that. Change the linear layout orientation to horizontal.
Upvotes: 2