Reputation: 299
in my project i have a view with 4 buttons. I have a relative layout to place them correctly. The thing is that i want the buttons to fill the whole screen without disappearing and I don't want to set a fixed size to them to ensure that the buttons will fit in any screen size. Right now the only thing i can see is one big button because RelativeLayout has no android:layout_weight
. How can i accomplish this? Here's what i got:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/search_icon"
android:background="@drawable/search_icon"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<Button
android:id="@+id/categories_icon"
android:background="@drawable/categories"
android:layout_toRightOf="@+id/search_icon"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<Button
android:id="@+id/green_icon"
android:background="@drawable/green_icon"
android:layout_below="@+id/search_icon"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<Button
android:id="@+id/red_icon"
android:background="@drawable/red_icon"
android:layout_toRightOf="@+id/green_icon"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
Upvotes: 1
Views: 9014
Reputation: 2380
Read this guy's blog if you have any of these problems: http://blog.stylingandroid.com/
Lots of examples, and explains things like weight really well.
Upvotes: 0
Reputation: 4971
Well, it seems easier than I thought... I think you are looking for this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_>
<Button android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="1" />
<Button android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="2" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="3" />
<Button android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="4" />
</LinearLayout>
Try it out, it should look like this:
Upvotes: 2
Reputation: 33509
Is this what you are looking for?
Here is the code I did for this layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:text="BUTTON 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:text="BUTTON 2"
android:layout_toRightOf="@+id/search_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:text="BUTTON 3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
<Button
android:text="BUTTON 4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
Upvotes: 1
Reputation: 4971
The thing about RelativeLayout is that you have to tell where to put the itens. Example:
<Button android:id="@+id/buyTicket"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Buy ticket"
android:layout_above="@id/LinearLayout02" />
<ImageView android:id="@+id/logo"
android:src="@drawable/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/buyTicket" />
Pay attention to android:layout_above="@id/buyTicket". If you want button A on top of button B, you first declare button A (for example), align it to top of parent, and then at the button B declaration, you say you want it under button A.
In your case, you have to declare button A, B, C and D, then set android:layout_alignParentTop="true"
in button A, and then set android:layout_bellow
on B, C and D (to the one before them). And don't forget to set height and width to wrap_content
.
Upvotes: 0