Подтвердить что ты не робот

Как центрировать взгляд с помощью RelativeLayout?

Я хотел знать, как центрировать представление между двумя другими представлениями (или между представлением и родительским концом) с помощью RelativeLayout.

Например, если у меня есть следующее...

enter image description here

Как вертикально центрировать кнопку между ImageView и нижней частью экрана, используя RelativeLayout?

Я ищу решение, где...

  • Кнопка не растягивается каким-либо образом
  • нет вложенных макетов

И я пытаюсь сделать это в XML-макете (не программно).

4b9b3361

Ответ 1

Вы можете использовать следующее:

<RelativeLayout 
        android:layout_below="@id/theImageView"
        android:align_parentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="200dp" >    


        <Button 
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"            
            android:onClick="onClickButton"
            android:textSize="20sp"
            android:text="Go"/>

    </RelativeLayout>

Ответ 2

Используйте ниже код XMl для этого, он решит вашу проблему.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/mLlayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/mImgView1"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="2" >

            <Button
                android:id="@+id/Btn1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="Dipak" />
        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>

Ответ 3

К сожалению, нет простого способа сделать это. Вы можете либо планировать макеты, либо делать это во время выполнения. Самый простой способ - раскрыть макеты:

<LinearLayout
    android:width="match_parent"
    android:height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/my_top_image"/>
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="@string/my_button_label"/>
    </RelativeLayout>
</LinearLayout>

Это помещает изображение вверху. Ниже атрибуты layout_height=0 и layout_weight=1 на RelativeLayout заставляют занять все оставшееся пространство. Затем вы можете центрировать кнопку в RelativeLayout. Вы можете играть с пропиской на кнопке, чтобы получить ее до нужного размера.

Ответ 4

используйте этот android: layout_centerInParent = "true" в файле XML.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1.0" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="0.8"
        android:background="#00ff00" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="0.2" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Button" />
    </RelativeLayout>

</LinearLayout>

Ответ 5

@Gus вы можете создать центральное представление между двумя другими представлениями... это неточно, вы должны попробовать  таким образом....

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

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="#00ccFF"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="59dp"
        android:layout_height="59dp"
        android:layout_alignRight="@+id/linearLayout1"
        android:layout_alignTop="@+id/linearLayout1"
        android:layout_marginRight="-21dp"
        android:layout_marginTop="-21dp"
        android:background="#FFccFF"
        android:orientation="vertical" >
    </LinearLayout>

</RelativeLayout>

Ответ 6

Я думаю, что вам просто нужно назначить изображение как обычно, и выровнять кнопку с помощью layout_below, не нужно для жесткого кодирования, а для использования вложенных представлений использовать андроид: gravity = "center" на кнопка, с которой вы закончили

   <RelativeLayout 
    android:layout_below="@id/theImageView"
    android:align_parentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="200dp" >    

   <ImageView
       android:id="@+id/imgview"
        android:layout_width="match_parentmat"
        android:layout_height="wrap_content"  />
    <Button 
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_below="@+id/imgview"
        android:gravity="center"        
        android:onClick="onClickButton"
        android:textSize="20sp"
        android:text="Go"/>

</RelativeLayout>

Ответ 7

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:src="@drawable/wood" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_below="@+id/imageView1"
        android:layout_marginLeft="101dp"
        android:layout_marginTop="53dp"
        android:text="Image Button" />
</RelativeLayout>