Равномерно выровнять кнопки по низу в Android для всех размеров экрана

#android #android-studio #android-layout #user-interface #android-fragments

#Android #android-студия #android-макет #пользовательский интерфейс #android-фрагменты

Вопрос:

У меня есть простой вид с одним изображением и одной нижней панелью навигации. Я изо всех сил пытаюсь подогнать все кнопки под все размеры экрана.

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@ id/simpleImageView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dp"
        android:src="@drawable/image1" />

    <LinearLayout
        android:id="@ id/navigationBar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp">

        <ImageButton
            android:id="@ id/icon_left"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginEnd="5dp"
            android:background="@drawable/icon_left" />

        <TextView
            android:id="@ id/text_spinner"
            android:layout_width="60dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginEnd="5dp"
            android:textAlignment="center"
            android:gravity="center"
            android:textColor="#000"
            android:textSize="20sp"
            android:text="Page: " />

        <Spinner
            android:id="@ id/page_dropdown"
            android:layout_height="50dp"
            android:layout_width="110dp"
            android:dropDownWidth="110dp"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginEnd="5dp"
            android:gravity="center"
            android:textAlignment="center" />

        <ImageButton
            android:id="@ id/bookmark"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginEnd="5dp"
            android:background="@drawable/star_hollow" />

        <ImageButton
            android:id="@ id/icon_right"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginEnd="5dp"
            android:background="@drawable/icon_right" />

    </LinearLayout>

</RelativeLayout>
 

Здесь вот как это выглядит в разрешении 480 x 800

Здесь так это выглядит в разрешении 768 x 1280

Здесь вот как это выглядит в разрешении 1080 x 1920

Мне нужно решение, в котором все эти кнопки, элементы управления равномерно распределены по всем размерам экрана.

Ответ №1:

Удалите LinearLayout и продолжайте использовать RelativeLayout для родительского макета. используйте layout_below атрибут для размещения кнопок и значков под изображением, а также используйте layout_toEndOf атрибут для размещения компонента справа от других компонентов.

Ссылка : https://developer.android.com/guide/topics/ui/layout/relative

Ответ №2:

Не используйте RelativeLayout. Он устарел и должен быть заменен на ConstraintLayout

В ConstraintLayout выровняйте внешние нижние части с атрибутами

 app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" // right side of button
app:layout_constraintStart_toStartOf="parent" // left side of button
 

и внутренние с

 app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/leftButton"
app:layout_constraintEnd_toStartOf="@id/rightButton"
 

Узнайте больше о ConstraintLayout