В Android, где разместить код для вычисления пользовательского макета? - PullRequest
1 голос
/ 14 июля 2020

У меня в голове макет, который я не знаю, как создать с помощью LinearLayout или ConstraintLayout. Поэтому я хотел бы написать код, чтобы делать это вручную. Код будет смотреть на размер экрана и размеры кнопок, а также вычислять и устанавливать позиции кнопок. Это возможно? Где бы я поместил этот код в свой подкласс Fragment?

Ниже представлен макет, который я собираюсь использовать. В iOS я бы использовал express с «ограничениями», но я новичок в Android и не знаю, как это сделать. Если есть - это способ сделать это без кода Java / Kotlin вне файлов макета XML, я все равно хотел бы знать, где этот пользовательский код будет go.

На приведенном ниже экране я бы хотел:

  1. Минимальное боковое отступление вокруг текста кнопки.
  2. Все кнопки одинаковой ширины.
  3. Если шрифт по умолчанию увеличен, поэтому кнопки заполняют экран, я могу отказаться от бокового отступа, прежде чем обрезать текст кнопки.

введите описание изображения здесь

1 Ответ

2 голосов
/ 14 июля 2020

Вы можете сделать это в файле XML следующим образом:

<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/mainScreen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp"
    tools:context=".MainActivity">


    <TextView
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:gravity="center"
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Some label text, blah blah blah blah"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button 1"
        app:layout_constraintBottom_toTopOf="@+id/button2"
        app:layout_constraintEnd_toStartOf="@+id/guidelineRight"
        app:layout_constraintStart_toStartOf="@+id/guidelineLeft"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button 2"
        app:layout_constraintBottom_toTopOf="@+id/button3"
        app:layout_constraintEnd_toStartOf="@+id/guidelineRight"
        app:layout_constraintStart_toStartOf="@+id/guidelineLeft"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button 3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guidelineRight"
        app:layout_constraintStart_toStartOf="@+id/guidelineLeft"
        app:layout_constraintTop_toBottomOf="@+id/button2" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.2" />


    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.8" />

</androidx.constraintlayout.widget.ConstraintLayout>

И эффект будет выглядеть так:

effect

You can change guidelines percent to set the width of buttons which You want. To expand them equally I use chains Видео YT с кратким объяснением

...