Can I use layout_weight to position a RelativeLayout?

后端 未结 3 1715
抹茶落季
抹茶落季 2021-02-13 13:37

I\'ve been trying all day to get this to work and i think that I can use RelativeLayout

android:layout_weight=\"0.3\"  

to pl

相关标签:
3条回答
  • 2021-02-13 14:18

    Since this xml file doesn't give error it doesn't mean that you can use these attributes (weight, orientation associated to LinearLayout) with RelativeLayout. Please see the documentation for RelativeLayout first.

    0 讨论(0)
  • 2021-02-13 14:20

    Stack 2 LinearLayout (L1, L2) sections with weights of 0.3 and 0.7 (and heights of 0dp) inside a LinearLayout (L0) with vertical orientation and total weight of 1.0. Put your buttons in the 0.7 weighted layout (L2). That will give you your spacing of 30% above the buttons.

    You can then place the containing LinearLayout (L0) inside a RelativeLayout (R0) and place L0 relative to the right of the parent (R0), which will position the entire thing on the right side of the screen.

    Edit: My version is pretty much same as Milde in end, except using a root of RelativeLayout so I could put the layout_alignParentRight="true" to ensure it's flush to the right. The only sticking point I found was ensuring your first LinearLayout has height of fill_parent so that your spacer really is 30% of screen.

    <?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">
        <LinearLayout android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="wrap_content" android:weightSum="1.0" android:layout_alignParentRight="true">
            <LinearLayout android:layout_weight="0.3" android:layout_height="0dp" android:layout_width="0dp" />
            <LinearLayout android:layout_weight="0.7" android:layout_height="0dp" android:layout_width="wrap_content" android:orientation="vertical">
                <Button android:text="Button 1" android:layout_width="wrap_content" android:layout_height="wrap_content" />
                <Button android:text="Button 2" android:layout_width="wrap_content" android:layout_height="wrap_content" />
                <Button android:text="Button 3" android:layout_width="wrap_content" android:layout_height="wrap_content" />
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>
    
    0 讨论(0)
  • 2021-02-13 14:23

    Try this:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/rightLinerLayoutL0"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        >
        <LinearLayout android:id="@+id/rightLinerLayoutL1" android:layout_weight="0.3" android:layout_width="fill_parent">
        </LinearLayout>
        <LinearLayout android:orientation="vertical" android:id="@+id/rightLinerLayoutL2" android:layout_weight="0.7" android:layout_width="fill_parent">
            <Button android:text="Button_A" android:layout_height="wrap_content" android:id="@+id/btn_A" android:layout_width="wrap_content" android:layout_gravity="right"></Button>
            <Button android:text="Button_B" android:layout_height="wrap_content" android:id="@+id/btn_B" android:layout_width="wrap_content" android:layout_gravity="right"></Button>
            <Button android:text="Button_C" android:layout_height="wrap_content" android:id="@+id/btn_C" android:layout_width="wrap_content" android:layout_gravity="right"></Button>
        </LinearLayout>
    </LinearLayout >
    

    enter image description here

    0 讨论(0)
提交回复
热议问题