Есть ли способ сместить вид из центра?

Я пытаюсь расположить свою кнопку не точно по центру, а, скажем, на 2/5 высоты экрана, я безуспешно искал атрибут, поэтому я попробовал этот подход

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:padding="20dp" >

 <ImageButton
    android:id="@+id/flashlight_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_above="@+id/fakeView"
    android:background="@null"
    android:contentDescription="@string/flashlight_button_description"
    android:src="@drawable/freeml_bright" />

   <View
    android:id="@+id/fakeView"
    android:layout_width="10dp"
    android:layout_height="10dp"
    android:layout_centerInParent="true"
    android:background="#FFAABB" />

</RelativeLayout>

Однако это не работает, даже если я устанавливаю маржу на фальшивом представлении.

Атрибут заполнения работает, однако, поскольку это большое изображение, и если я хочу, чтобы оно начиналось с 2/5 высоты экрана, оно покрывало центральную точку экрана, поэтому, если я использую атрибут заполнения, он работает, но отталкивает его от центра и не позволяет его покрыть.

Хотя я заставил это работать, используя LinearLayout, чего я хотел избежать, потому что есть больше представлений сверху и снизу рядом друг с другом, что привело бы к вложенным представлениям с использованием линейной компоновки. К сожалению, я думаю, что это единственный вариант.

По сути, он использует другой линейный макет, который заполняет оставшееся пространство, оставшееся неиспользованным видами сверху и снизу, с height=0dpи weight=1и устанавливает его гравитацию в центр.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/application_logo_description"
        android:src="@drawable/mylight" />

     <ImageButton
        android:id="@+id/settings_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:background="@null"
        android:contentDescription="@string/settings_button_description"
        android:src="@drawable/settings_button" /> 
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageButton
        android:id="@+id/flashlight_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:contentDescription="@string/flashlight_button_description"
        android:src="@drawable/flashlight_button_selector" />

    <View
        android:id="@+id/fakeView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="60dp"
        android:background="#FFAABB" />
</LinearLayout>

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|center_horizontal"
    android:contentDescription="@string/powered_by_description"
    android:src="@drawable/powered_by" />

<ImageButton
    android:id="@+id/ad_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|center_horizontal"
    android:background="@null"
    android:contentDescription="@string/ad_button_description"
    android:src="@drawable/freeml" />

 </LinearLayout>
24
задан double-beep 9 September 2019 в 07:07
поделиться