简体   繁体   中英

Layout weight in relative layout

I am aware about layout weight in linear layout. Can i assign layout weight in relative layout.

example: two image view in a layout which fills the layout in the ratio 60:40. first image should take up 60% of the whole screen height and the second image has to take the remaining 40% of the screen.

Don't just answer for this example problem alone please tell me the concept precisely or post some reference links about layout weight in relative layout. Thanks in advance.

You can place an invisible view in center of your layout and align your view in left and right. Here is an example

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@+id/view"
        android:background="#fffba2" />

    <View 
        android:id="@+id/view"
        android:layout_width="1dp"
        android:layout_height="1dp"
        android:layout_centerHorizontal="true"
        android:visibility="invisible" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toRightOf="@+id/view"
        android:background="#ba2fff" />

</RelativeLayout>

Actually you cannot use weight in a RelativeLayout but you can use a combination of Relative and Linear layouts in order to take both advantages of them!

Tip: Try to use as less layouts as possible to avoid slow UI, because of multiple screen measurements! [1] [2]

There is no need of weights with Relative Layout. You can move around the Image Views to make sure that they are in correct proportion. Weights are only used with LinearLayout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM