Размещение ImageView слева от центрированных кнопок

Я пытаюсь разместить объект ImageView слева от объекта линейного макета (который является родителем двух кнопок). Я в основном хочу, чтобы две кнопки располагались по центру по горизонтали, а экран с изображением располагался слева от центрированных кнопок. Я по-прежнему хочу, чтобы кнопки оставались центрированными по горизонтали, размещенное изображение не должно быть центрировано, просто сбоку.

Любые предложения были бы действительно замечательными. Возможно, я подходил к своей проблеме с неправильными макетами, но вот что я пробовал:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/gameSettingsContainer"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.vb1115.multchoicequestion.LaunchScreen"
    android:orientation="vertical"
    android:gravity="center_horizontal">
    <LinearLayout
        android:id="@+id/buttonContainer"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ToggleButton android:id="@+id/mathButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOff="Math"
            android:textOn="Math"
            android:text="Math"/>
        <ToggleButton android:id="@+id/bioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOff="Biology"
            android:textOn="Biology"
            android:text="Biology"/>
    </LinearLayout>
    <ImageView
        android:id="@+id/animatedArrow"
        android:src="@mipmap/green_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/buttonContainer"/>
</RelativeLayout>

person Vik    schedule 08.02.2016    source источник


Ответы (3)


Хотя у меня нет Android Studio для тестирования, я уже могу сделать несколько предположений...

Во-первых, нет необходимости использовать android:orientation="vertical" или android:gravity="center_horizontal" для вашего корневого RelativeLayout...

2-й, чтобы расположить 2 дочерних элемента, используйте android:layout_centerHorizontal для LinearLayout и сохраните android:layout_alignParentLeft для вашего ImageView...

Это должно стать немного лучше... Основная идея заключается в том, что в RelativeLayout обычно более эффективно позволить детям определять свою позицию... Надеюсь, это поможет.

person JBA    schedule 08.02.2016
comment
это работает, спасибо!! Также оцените разъяснения. - person Vik; 09.02.2016

Вот решение

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/gameSettingsContainer"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:id="@+id/buttonContainer"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ToggleButton
            android:id="@+id/mathButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Math"
            android:textOff="Math"
            android:textOn="Math"/>

        <ToggleButton
            android:id="@+id/bioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Biology"
            android:textOff="Biology"
            android:textOn="Biology"/>
    </LinearLayout>

    <ImageView
        android:id="@+id/animatedArrow"
        android:layout_toStartOf="@+id/buttonContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/green_arrow"/>
</RelativeLayout>
person Skalaw    schedule 08.02.2016

Без тестирования Android-студий:

Чтобы центрировать LinearLayout внутри RelativeLayout, используйте Layout_centerHorizontal="true"

<LinearLayout
    android:id="@+id/buttonContainer"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true">

Чтобы каждая из кнопок была распределена равномерно и по центру, используйте layout_weight="1" (чтобы все они имели одинаковый вес) иgravity="center_horizontal"

    <ToggleButton android:id="@+id/mathButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="Math"
        android:textOn="Math"
        android:text="Math"
        android:layout_weight="1"
        android:layout_gravity="center_horizontal"/>
    <ToggleButton android:id="@+id/bioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="Biology"
        android:textOn="Biology"
        android:text="Biology"
        android:layout_weight="1"
        android:layout_gravity="center_horizontal"/>

Затем поместите ImageView в layout_toStartOf вашего buttonContainer.

<ImageView
        android:id="@+id/animatedArrow"
        android:src="@mipmap/green_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toStartOf="@+id/buttonContainer">
person Helix    schedule 08.02.2016