Могу ли я использовать layout_weight для позиционирования RelativeLayout? - PullRequest
12 голосов
/ 21 июня 2011

Я целый день пытался заставить это работать, и я думаю, что могу использовать RelativeLayout

android:layout_weight="0.3"  

, чтобы разместить три кнопки на правой стороне экрана, не по центру, а 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>

[ОБНОВЛЕНИЕ] Добавлена ​​окончательная рабочая версия для людей, которым она нужна

<?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>

Ответы [ 3 ]

7 голосов
/ 21 июня 2011

Stack 2 сечения LinearLayout (L1, L2) с весами 0,3 и 0,7 (и высотой 0dp) внутри LinearLayout (L0) с вертикальной ориентацией и общим весом 1,0.Поместите свои кнопки в 0,7 взвешенную раскладку (L2).Это даст вам интервал 30% над кнопками.

Затем вы можете поместить содержащий LinearLayout (L0) внутри RelativeLayout (R0) и поместить L0 относительно правого родительского элемента (R0),поместит всю вещь на правой стороне экрана.

Редактировать: Моя версия во многом похожа на Milde в конце, за исключением использования корня 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>
2 голосов
/ 21 июня 2011

Попробуйте это:

<?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 голосов
/ 21 июня 2011

Поскольку этот XML-файл не выдает ошибку, это не означает, что вы можете использовать эти атрибуты (вес, ориентация, связанные с LinearLayout) с RelativeLayout.Пожалуйста, ознакомьтесь с документацией для RelativeLayout в первую очередь.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...