Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RelativeLayout weight

Tags:

In a layout resource XML, I have 3 RelativeLayout(s) which are inside a main RelativeLayout. The view will be shown vertically. These 3 RelativeLayout() are set next to each other, and I want them to fill the whole screen, doesnt matter what will be the screen size. My, layout view:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/backg"  >  <ImageView     android:id="@+id/imageView1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignParentLeft="true"     android:layout_alignParentTop="true"     android:layout_marginTop="@dimen/top_mr_image"     android:src="@drawable/temp" />  <RelativeLayout     android:id="@+id/r1"     android:layout_width="0dp"     android:layout_height="wrap_content"     android:layout_alignParentBottom="true"     android:layout_alignParentLeft="true"     android:layout_below="@+id/imageView1"     android:layout_marginLeft="10dp"     android:background="@drawable/r1bg"     android:layout_weight="1"       >      <TextView         android:id="@+id/textView1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentLeft="true"         android:layout_marginLeft="@dimen/txt_mr_right"         android:layout_marginRight="@dimen/txt_mr_right"         android:layout_marginTop="39dp"         android:text="S"         android:textAppearance="?android:attr/textAppearanceLarge" />      <TextView         android:id="@+id/textView2"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentLeft="true"         android:layout_below="@id/textView1"         android:layout_marginLeft="@dimen/txt_mr_right"         android:layout_marginRight="@dimen/txt_mr_right"         android:text="T"         android:textAppearance="?android:attr/textAppearanceLarge" /> </RelativeLayout>      <RelativeLayout         android:id="@+id/r2"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:layout_alignParentBottom="true"         android:layout_alignTop="@+id/r1"         android:layout_toRightOf="@+id/r1"         android:layout_weight="1" >     </RelativeLayout>              <RelativeLayout         android:id="@+id/r3"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:layout_alignParentBottom="true"         android:layout_alignTop="@+id/r2"         android:layout_toRightOf="@+id/r2"         android:layout_weight="1" >      </RelativeLayout> 

I set weight=1 and layout_width=0dp for each relativeLayout and this technique works with buttons, I thought the same will be with relativeLayout, seems my thoughts were wrong. Any idea?

UPD1: I have added an image of what I would like to have

enter image description here

like image 276
Daler Avatar asked Dec 23 '12 06:12

Daler


People also ask

Can we use weight in RelativeLayout?

RelativeLayouts do not support weight. You need to use a LinearLayout as a parent container if you want to use weights.

Which is better LinearLayout or RelativeLayout?

LinearLayout is less used as compared to RelativeLayout. RelativeLayout is used more in applications. We can use LinearLayout inside RelativeLayout. We can also use RelativeLayout as a Child of LinearLayout.

What is difference between LinearLayout and RelativeLayout?

LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets.

Is RelativeLayout deprecated?

relativelayout is deprecated now.


2 Answers

RelativeLayout does not pay attention to android:layout_weight. (That's a property of LinearLayout.LayoutParams, but not of RelativeLayout.LayoutParams.)

You should be able to get the layout you want with a much simpler view hierarchy. It's not clear what you are trying to do, since the last two RelativeLayouts are empty. If you need a purely vertical organization, I'd suggest using LinearLayout instead of RelativeLayout.

EDIT Based on your edit, it looks like you want a horizontal layout of three compound views, each one clickable. I think something like the following will work:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="horizontal"     android:layout_width="match_parent"     android:layout_height="match_parent"     >      <!-- First column -->     <LinearLayout         android:id="@+id/firstColumn"         android:orientation="vertical"         android:gravity="center_horizontal"         android:layout_width="0dp"         android:layout_height="match_parent"         android:layout_weight="1"         >          <ImageView             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:src="..." />         <TextView             android:layout_width="wrap_content"             android:layout_height="match_parent"             android:text="text 1"             . . . />     </LinearLayout>      <!-- Second column -->     <LinearLayout . . . >         . . .     </LinearLayout> </LinearLayout> 

If the contents of the buttons aren't correct, you can replace the second-level LinearLayout views with RelativeLayout if that helps organize the layout better.

like image 61
Ted Hopp Avatar answered Sep 20 '22 14:09

Ted Hopp


RelativeLayouts do not support weight. You need to use a LinearLayout as a parent container if you want to use weights.

like image 20
dmon Avatar answered Sep 20 '22 14:09

dmon