Code Droid
Code Droid

Reputation: 10472

Android Layout Setting Height to a specific percentage of its container?

I would like to set an inner layout that is included in a relative layout to a specific percentage of the layout it is included in. How can I set the layout height to a specific percentage. Another example lets say I have an ImageView and I want it to take up exactly 60% of the container that it is within. How can I specify this? I did see: Android Layout Percentage/Proportion Resize which somewhat explains this. However if I wanted say 48% I am not sure adjust weight would work?

Upvotes: 0

Views: 3642

Answers (1)

Chopin
Chopin

Reputation: 1452

You'll need to use LinearLayout instead, with the property android:layout_weigth.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="5"
    android:orientation="vertical" >
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="2" >

    <!-- Other elements here -->

    </RelativeLayout>
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="3" />
</LinearLayout>

The nested RelativeLayout will ocuppy 2 / (2 + 3) = 0.4 (40%), and the ImageView 3 / (2 + 3) = 0.6 (60%).

Upvotes: 3

Related Questions