1

Четыре кнопки

Как проще всего сделать так, чтобы кнопки сами отмасштабировались под заданную часть экрана (например, под 25% экрана)? Чтобы, например, если я захочу добавить шесть кнопок, а не четыре, как на рисунке, то у меня кнопки автоматически бы распредилили между собой отведённое место и образовали три ряда в два столбика? Какой layout нужно использовать для этого?

3 ответа 3

2

Можно по разному. Пожалуй, самый простой в реализации способ такой:

<LinearLayout android:orientation="vertical">

    <LinearLayout android:orientation="horizontal">

        <View android:width="0dp" android:weight="1"/>
        <View android:width="0dp" android:weight="1"/>

    </LinearLayout>

    <LinearLayout android:orientation="horizontal">

        <View android:width="0dp" android:weight="1"/>
        <View android:width="0dp" android:weight="1"/>

    </LinearLayout>

</LinearLayout>
1
  • 1
    Весы - это классика, и никакие ConstraintLayout не нужны. Четко, кратко и по существу :)
    – iFr0z
    6 ноя 2019 в 2:49
1

Поскольку вам нужно расширяемое решение и количество view внутри не известно, попробуйте использовать GridLayout. Программно помещайте в него ваши кнопки и пользуясь свойствами setColumnCount(int columnCount); setRowCount(int rowCount) размещайте элементы как вам нужно. Более подробно в документации https://developer.android.com/reference/android/widget/GridLayout .

Так же вы можете задать общий отступ между элементами в android:useDefaultMargins="true" или задавать свойства для каждого view отдельно, но не забудьте вернуть по умолчанию в android:useDefaultMargins="false"

0

Через ConstraintLayout:
Нужно 3 Guideline для привязки объектов к ним, 2 горизонтальные на 75% и 87,5% от высоты экрана, и одна вертикальная на 50% от ширины экрана.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintGuide_percent="0.75"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="Button"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline2"
        app:layout_constraintTop_toTopOf="@+id/guideline" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline2"
        app:layout_constraintTop_toTopOf="@+id/guideline3" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="Button"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"
        app:layout_constraintEnd_toStartOf="@+id/guideline2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline" />

    <Button
        android:id="@+id/button4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline3" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.875" />
</android.support.constraint.ConstraintLayout>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.