Переключение вправо от TextInputLayout Android - PullRequest
0 голосов
/ 19 ноября 2018

Я хотел бы разместить переключатель справа от TextInputLayout с параметрами match_parent на TextInput и полем.

Кажется, что установка переключателя влево работает, однако при размещении вправо он скрыт.

См. Два изображения ниже:

Switch to the left

Switch to the right

Как видите, коммутатор размещается неправильно.

Вот мой код XML.Основной макет является CoordinatorLayout:

                    <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Social Networks"
                        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
                        android:textColor="@color/colorPrimary"
                        android:textStyle="bold" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <android.support.design.widget.TextInputLayout
                            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginEnd="40dp">

                            <android.support.design.widget.TextInputEditText
                                android:id="@+id/myprofile_google"
                                android:inputType="textEmailAddress"
                                android:layout_width="match_parent"
                                android:textColor="@color/blue_grey_800"
                                android:layout_height="wrap_content"
                                android:hint="Google +" />

                        </android.support.design.widget.TextInputLayout>

                        <Switch
                            android:layout_width="wrap_content"
                            android:layout_height="30dp"
                            android:layout_gravity="center_vertical"/>



                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>

Ответы [ 3 ]

0 голосов
/ 19 ноября 2018

Измените layout_width вашего TextInputLayout на 0dp и добавьте к нему также этот атрибут:

android:layout_weight="1"

Что сейчас происходит, так это то, что ваш TextInputLayout имеет ширину match_parent. Для этого нужно заполнить все оставшееся пространство родителя (это несколько уникально для LinearLayout). Когда ваш Switch слева, он получает достаточно места для него, а затем TextInputLayout берет на себя все остальное. Однако, когда коммутатор находится справа, TextInputLayout сначала занимает все пространство!

Использование веса вместо этого гарантирует, что другие компоненты получат необходимое им пространство до того, как взвешенный компонент получит дополнительное пространство.

0 голосов
/ 19 ноября 2018

Я бы предложил вам использовать RelativeLayout вместо LinearLayout или, что еще лучше, переделать весь макет в ConstraintLayout.

Реализация RelativeLayout, вероятно, будет выглядеть примерно так

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <android.support.design.widget.TextInputLayout
                        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                        android:id="@+id/textInputLayout"
                        android:layout_toStartOf="@+id/switch"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="40dp">

                        <android.support.design.widget.TextInputEditText
                            android:id="@+id/myprofile_google"
                            android:inputType="textEmailAddress"
                            android:layout_width="match_parent"
                            android:textColor="@color/blue_grey_800"
                            android:layout_height="wrap_content"
                            android:hint="Google +" />

                    </android.support.design.widget.TextInputLayout>

                    <Switch
                        android:id="@+id/switch"
                        android:layout_width="wrap_content"
                        android:layout_height="30dp"
                        android:layout_alignParentEnd="true"
                        android:layout_gravity="center_vertical"/>



                </LinearLayout>
0 голосов
/ 19 ноября 2018

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

Этот макет создает желаемый результат:

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Social Networks"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textColor="@color/colorPrimary"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:layout_height="match_parent">

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginEnd="40dp">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/myprofile_google"
                    android:inputType="textEmailAddress"
                    android:layout_width="match_parent"
                    android:textColor="@android:color/holo_red_dark"
                    android:layout_height="wrap_content"
                    android:hint="Google +" />

            </android.support.design.widget.TextInputLayout>

            <Switch
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:layout_gravity="center_vertical"/>



        </LinearLayout>

    </LinearLayout>
...