Chux
Chux

Reputation: 1227

Create a layout based in percentages

I'm trying to do my first app.

I'm doing a layout that contains four rows. There will be a title in the first row (10% height), two images in the second (40% height), two images in the third (40% height) and a button in the fourth (10% height).

Right now, I'm using a linear layout with vertical orientation. Using weight sum and weight, I have the correct proportion on each row.

But, if I use in the second and the third rows linear layout weight, then I get a warning about nested weights and bad performance. I understand the bad performance issue, but I don't know how to solve my problem without them.

I need each image be a 50% of its parent width.

Thanks for your help.

EDIT: That is a quick mockup of what i'm trying to accomplish https://dl.dropbox.com/u/252856/androidlayout.jpg

Upvotes: 2

Views: 175

Answers (2)

user
user

Reputation: 87064

In your particular situation you could make the two ImageViews occupy 50% of the parent's width without using weights with a block like this:

<RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" 
     android:layout_weight="your_value">
     <View android:id="@+id/anchor" android:layout_width="0dp" 
           android:layout_height="match_parent" android:layout_centerHorizontal="true"/>
     <ImageView android:layout_width="wrap_content" 
           android:layout_height="match_parent" android:layout_alignParentLeft="true"
           android:layout_alignRight="@id/anchor" /> 
     <ImageView android:layout_width="wrap_content" 
           android:layout_height="match_parent" android:layout_alignParentRight="true"
           android:layout_alignLeft="@id/anchor" />
</RelativeLayout>

Upvotes: 2

Cloudy
Cloudy

Reputation: 2411

You may want to take a look and consider whether you could make your layout work as a GridLayout. It's designed to handle situations where you want to arrange UI elements into a roughly grid-like pattern, while avoiding the performance issues that nested weights can cause. If your UI lends itself to this sort of layout, you can achieve both simpler implementation and faster performance by taking advantage of it.

GridLayout is supported as far back as API level 7, I believe, via the support library.

Upvotes: 0

Related Questions