user1938357
user1938357

Reputation: 1466

getting the layout weights right

Need help with a layout xml. When I test the below dimensions for the below xml layout in my code for height of textview(by using textview.getHeight) versus height of screen(by DisplayMetrics.getMetrics()), it comes as 78.9%, meaning the height of textview is 78.9% of screenheight. I was expecting this should be 90% as I have assigned weight 1 and 9 to my two relativelayouts which have textview and other components(buttons) respectively. Can anyone please help. My objective was to make the textview 10% of the screenheight.

<LinearLayout 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:orientation="vertical"
tools:context=".Story1Activity" >

<RelativeLayout
    android:layout_weight="1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/textViewStory1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:lineSpacingMultiplier="1.2" />
</RelativeLayout>

<RelativeLayout
    android:layout_weight="9"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:id="@+id/buttonPrevious"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:background="@android:drawable/btn_default"
        android:text="@string/stringButtonPrevious"
        android:onClick="loadPrevious" />

    <Button
        android:id="@+id/buttonNext"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:background="@android:drawable/btn_default"
        android:text="@string/stringButtonNext" 
        android:onClick="loadNext"/>

Upvotes: 0

Views: 111

Answers (1)

Dan Dyer
Dan Dyer

Reputation: 54475

Set the heights of each RelativeLayout to zero and the 'spare' space (all 100% of it) will be allocated according to the weights. If you start with non-zero sizes, the weights are only used to adjust the widget sizes after they have been assigned their normal sizes.

Upvotes: 2

Related Questions