将标签放置在具有特定设计的EditText中

时间:2019-02-04 09:29:07

标签: android android-edittext textview

我需要为Android重写iOS应用。布局看起来应该相似,而我为此问题苦苦挣扎:

我想在EditText的左侧有一个标签。它应该粘贴在那里,并且不要让EditText文本覆盖标签:

Label for EditText

我尝试像这样在Android Studio中使用界面生成器: enter image description here

问题在于,EditText的文本没有隐藏在标签后面。 有什么想法吗?

编辑: 由于我的问题不清楚:

我想要一个EditText内的标签。 它应保持在左侧,并在EditText的输入处留有空白。

1 个答案:

答案 0 :(得分:1)

尝试在xml中使用以下代码并进行检查,这会将标签文本与密码字段对齐,用户可以在其中输入密码。

<androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:drawable/editbox_background"
            android:layout_margin="@dimen/padding_medium">

        <TextView
                android:id="@+id/label"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:text="Password"
                android:layout_marginTop="@dimen/padding_medium"
                android:gravity="center_vertical"
                android:paddingLeft="@dimen/padding_xsmall"
                android:textSize="22sp"
                android:textColor="@android:color/holo_red_dark"
                app:layout_constraintHeight_default="wrap"
                android:paddingStart="@dimen/padding_xsmall"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintWidth_default="wrap"/>

            <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="0dp"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintLeft_toRightOf="@id/label"
                    app:layout_constraintRight_toRightOf="parent"
                    android:layout_marginStart="4dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="4dp"
                    android:background="@null"
                    android:padding="4dp"
                    android:imeOptions="actionNext"
                    android:lines="1"
                    android:inputType="textPassword"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

注意:我使用的是androidx控件,因为我正在使用新的材质设计库。如果您使用的是支持库,则需要用支持库中的控件替换它们。