首页 > 解决方案 > 如何在 android 的 TextInputLayout 中对齐文本?

问题描述

RelativeLayout我有一个TextInputLayout我在EditText里面输入电话号码的地方和一个我在Textview里面LinearLayout显示国家代码的地方。当我们开始输入 `TextInputLayout 时滑动到顶部,但我希望它像图 3 那样滑动到顶部。

我在这方面面临问题有人可以帮助我吗?这是我的代码:

<RelativeLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                >
        
                                <LinearLayout
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_alignStart="@+id/error_mobile_no"
                                    android:layout_alignTop="@+id/error_mobile_no"
                                    android:layout_alignBottom="@+id/error_mobile_no"
                                    android:layout_marginTop="0dp"
                                    android:layout_marginBottom="0dp"
                                    android:gravity="center"
                                    android:orientation="horizontal">
        
                                    <TextView
                                        android:id="@+id/country_code_textview"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:fontFamily="@font/lorin_regular"
                                        android:text="+971"
                                        android:layout_marginBottom="2dp"
                                        android:textColor="@color/black"
                                        android:textSize="14sp" />
                                </LinearLayout>
        
                                <com.google.android.material.textfield.TextInputLayout
                                    android:id="@+id/error_mobile_no"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_marginEnd="20dp"
                                    android:layout_marginStart="20dp"
                                    app:errorEnabled="true">
        
                                    <EditText
                                        android:id="@+id/etPhoneNumberDetailedActivity"
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:layout_gravity="start"
                                        android:autofillHints=""
                                        android:paddingStart="35dp"
                                        android:backgroundTint="#6283a3"
                                        android:fontFamily="@font/lorin_regular"
                                        android:hint="@string/mobile_number"
                                        android:inputType="phone"
                                        android:textColor="@color/black"
                                        android:textColorHint="#6283a3" android:textSize="14sp" />
                                </com.google.android.material.textfield.TextInputLayout>
                            </RelativeLayout>

这是输入文本之前的字段图像。在此处输入图像描述

这是输入文字后的图像。在此处输入图像描述

但我试图让它看起来像这样。 在此处输入图像描述

标签: androidmaterial-components-androidandroid-textinputlayoutmaterial-components

解决方案


您可以使用app:prefixText来显示前缀:

    <com.google.android.material.textfield.TextInputLayout
        android:hint="mobile_number"
        app:prefixText="+971"
        ...>

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:autofillHints=""
            android:inputType="phone"
            .../>

    </com.google.android.material.textfield.TextInputLayout>

在此处输入图像描述


推荐阅读