Макет Android: выровнять текст флажка и текстового представления в ConstraintLayout

#android #android-layout

#Android #android-макет

Вопрос:

Я хочу выровнять по левому краю тексты (не значок и флажок) текстового представления с помощью значка информации и флажка в ConstraintLayout приложения Android.

Значок информации немного больше флажка, поэтому текст флажка начинается немного левее текста TextView.

Иллюстрация в формате ASCII (я бы хотел, чтобы буква «T» в «This» была выровнена с буквой «T» в «The»):

 ( i )    This is the information text
[x]    The text of this Checkbox is not aligned with the text of the TextView
  

Извлечение XML-макета:

 <androidx.constraintlayout.widget.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="wrap_content">

    <TextView
        android:id="@ id/textInfo"
        style="@style/TextAppearance.AppCompat.Body1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:drawablePadding="8dp"
        android:text="This is some very important information that has to be aligned left"
        app:drawableStartCompat="@drawable/icon_info"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:text="This is the Text of the Checkbox"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textInfo" />
</androidx.constraintlayout.widget.ConstraintLayout>
  

Ответ №1:

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

Пример XML-макета:

 <androidx.constraintlayout.widget.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="wrap_content">

    <ImageView
        android:id="@ id/iv_info"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/ic_info_icon"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@ id/tv_info_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="@id/iv_info"
        app:layout_constraintBottom_toBottomOf="@id/iv_info"
        app:layout_constraintStart_toStartOf="@id/tv_checkbox_text"
        android:text="This is the info text"/>

    <CheckBox
        android:id="@ id/cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/iv_info"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@ id/tv_checkbox_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="@id/cb"
        app:layout_constraintBottom_toBottomOf="@id/cb"
        app:layout_constraintStart_toEndOf="@id/cb"
        android:layout_marginStart="20dp"
        android:text="This is the checkbox text"/>

</androidx.constraintlayout.widget.ConstraintLayout>
  

Изображение макета:

Макет

Обратите внимание, что вам нужно будет настроить начальное поле текстового представления, связанного с флажком, чтобы получить нужный интервал.

Ответ №2:

Я бы сделал это так: извлек текст в TextViews, добавил вертикальную направляющую и выровнял текст по этой направляющей.

Ответ №3:

это мой код

 <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@ id/layout_head"
        tools:layout_editor_absoluteX="15dp">


        <androidx.appcompat.widget.AppCompatButton
            android:id="@ id/transferBttnChooseRoom"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/pilih"
            android:textColor="@color/colorAccent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@ id/transferChoosedRoom"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="@string/pilih_room_transfer"
            android:textSize="15sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@ id/transferBttnChooseRoom"
            app:layout_constraintTop_toTopOf="parent"/>



    </androidx.constraintlayout.widget.ConstraintLayout>
  

может быть, вы можете изменить свой случай следующим образом