我可以使用layout_weight来定位RelativeLayout吗?

Eri*_*rik 12 android android-layout

我一直在努力让这个工作,我认为我可以使用 RelativeLayout

android:layout_weight="0.3"  
Run Code Online (Sandbox Code Playgroud)

在屏幕右侧放置三个按钮,不是居中,而是从顶部向下30%.

这是可能的,如果是这样,我该怎么做?

以下是我的XML,它显示了三个按钮在彼此之下,但在右上角位置:

   </RelativeLayout>
      <LinearLayout
        android:id="@+id/rightRelativeLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_weight="1"
        android:orientation="vertical"
        >
            <ImageButton
                android:id="@+id/btn_A" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="A"
                android:src="@drawable/drawer_1"
                android:background="@android:color/transparent"


            /> 
            <ImageButton
                android:id="@+id/btn_B" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:text="B"
                android:src="@drawable/drawer_1"
                android:background="@android:color/transparent"

                android:layout_below="@+id/btn_A"
            />
            <ImageButton
                android:id="@+id/btn_C" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginTop="10dp"
                android:text="C"
                android:src="@drawable/drawer_1"
                android:background="@android:color/transparent"

                android:layout_below="@+id/btn_B"

            />
        </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

[更新]为需要它的人添加了最终工作版本

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

    <Gallery xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/examplegallery" 
             android:layout_width="fill_parent"
             android:layout_height="wrap_content" />
   <RelativeLayout 
        android:id="@+id/InnerRelativeLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >
    <Button 
        android:id="@+id/btn_newpen_drawtext" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pen"
    />      
    <EditText 
        android:id="@+id/etx_addtext_drawtext"
        android:layout_width="fill_parent"
        android:layout_toLeftOf="@+id/btn_delete_pen"
        android:layout_toRightOf="@+id/btn_newpen_drawtext"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:text=""
    />
        <Button 
            android:id="@+id/btn_delete_pen" 
            android:layout_toLeftOf="@+id/btn_save_drawtext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Del"
        />
        <Button 
            android:id="@+id/btn_save_drawtext" 
            android:layout_alignParentRight="true" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="save"
        />

   </RelativeLayout>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rightLinerLayoutL0"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_toRightOf="@+id/RelativeLayout1"
    android:weightSum="1.0"
    android:layout_alignParentRight="true"
    android:orientation="vertical"
    >
    <LinearLayout android:layout_height="0dp" 
                  android:id="@+id/rightLinerLayoutL1" 
                  android:layout_weight="0.15" 
                  android:layout_width="0dp">
    </LinearLayout>
    <LinearLayout android:layout_height="0dp" 
                  android:orientation="vertical" 
                  android:id="@+id/rightLinerLayoutL2" 
                  android:layout_weight="0.85" 
                  android:layout_width="wrap_content">

        <ImageButton android:text="Button_A" 
            android:background="@android:color/transparent" 
            android:layout_height="wrap_content" 
            android:id="@+id/btn_A" 
            android:layout_width="wrap_content" 
            android:layout_gravity="right" 
            android:src="@drawable/drawer"
            android:onClick="btnAListener">
        </ImageButton>
        <ImageButton android:text="Button_B" 
            android:background="@android:color/transparent" 
            android:layout_height="wrap_content" 
            android:layout_marginTop="10dp"
            android:id="@+id/btn_B" 
            android:layout_width="wrap_content" 
            android:layout_gravity="right" 
            android:src="@drawable/drawer"
            android:onClick="btnBListener">
        </ImageButton>
        <ImageButton android:text="Button_C" 
            android:background="@android:color/transparent" 
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:id="@+id/btn_C" 
            android:layout_width="wrap_content" 
            android:layout_gravity="right" 
            android:src="@drawable/drawer"
            android:onClick="btnCListener">
        </ImageButton>
    </LinearLayout>
</LinearLayout >

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

Mar*_*her 7

在LinearLayout(L0)内堆叠2个LinearLayout(L1,L2)部分,权重为0.3和0.7(以及高度为0dp),垂直方向和总权重为1.0.将按钮置于0.7加权布局(L2).那将使你的按钮间距高出30%.

然后,您可以将包含LinearLayout(L0)放置在RelativeLayout(R0)内,并将L0相对于父级(R0)的右侧放置,这将使整个事物位于屏幕的右侧.

编辑:我的版本与米尔德完全相同,除了使用根,RelativeLayout所以我可以把layout_alignParentRight="true"它确保它向右冲洗.我发现的唯一一个问题就是确保你的第一个LinearLayout具有高度,fill_parent这样你的间隔物确实是屏幕的30%.

<?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>
Run Code Online (Sandbox Code Playgroud)