Search code examples
androidandroid-layoutxamarinpercentageandroid-relativelayout

How to adjust width of relative layouts in percent?


How can I adjust the width of my 2 Relative Layouts correctly, so that the one to the left takes 0.25 of screen space and the one to the right takes 0.75? Inside the second relative layout there's a table layout with 3 columns, each of them should take 1/3 of table's size.

Here's what I've tried with AXML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RelativeLayout
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="0.25">
        <LinearLayout
            android:id="@+id/buttonsLeft"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">
            <Button
                android:id="@+id/buttonBack"
                android:text="@string/btnBack"
                android:layout_width="@dimen/myBtnWidth"
                android:layout_height="@dimen/myBtnHeight"
                android:layout_marginTop="@dimen/myBtnMargin"
                android:layout_marginLeft="@dimen/myBtnMargin" />
            <Button
                android:id="@+id/buttonStart"
                android:text="@string/btnStart"
                android:layout_width="@dimen/myBtnWidth"
                android:layout_height="@dimen/myBtnHeight"
                android:layout_marginTop="@dimen/myBtnMargin"
                android:layout_marginLeft="@dimen/myBtnMargin" />
        </LinearLayout>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="0.75">
        <LinearLayout
            android:id="@+id/tableRight"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">
            <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/tableLayout1">
                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scrollbars="vertical">
                    <TextView
                        android:id="@+id/Text1"
                        android:padding="10sp"
                        android:textSize="@dimen/myTextSizeColHeader"
                        android:text="@string/bin"
                        android:layout_height="wrap_content"
                        android:layout_width="@dimen/myColWidthBin"
                        android:gravity="left" />
                    <TextView
                        android:id="@+id/Text2"
                        android:padding="10sp"
                        android:textSize="@dimen/myTextSizeColHeader"
                        android:text="@string/item_customer"
                        android:layout_height="wrap_content"
                        android:layout_width="@dimen/myColWidthItemCust"
                        android:gravity="center" />
                    <TextView
                        android:id="@+id/Text4"
                        android:padding="10sp"
                        android:textSize="@dimen/myTextSizeColHeader"
                        android:text="@string/nominal_actualQty"
                        android:layout_height="wrap_content"
                        android:layout_width="@dimen/myColWidthActNomQty"
                        android:gravity="right" />
                </TableRow>
                <ListView
                    android:id="@+id/whseActivHeadersList" />
            </TableLayout>
        </LinearLayout>
    </RelativeLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="52.5dp"
        android:id="@+id/notificationsNextorder"
        android:textSize="14sp"
        android:textStyle="bold" />
</LinearLayout>

Result: enter image description here


Solution

  • Add weightSum to parent linear layout. Check this answer

    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="4">
    
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">
    
        </RelativeLayout>
    
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3">
    
        </RelativeLayout>