How to group together a label and a textView together in Android?

谁都会走 提交于 2021-01-29 19:47:54

问题


I want to group together the label (e.g. "StaffID") and the value of the textView (e.g. "S1234567") together, vertically, in Android. The label stays on top the textView value throughout while the user is typing in the value in the textView. Attached is the screenshot of how I want the UI to look like.

Click here to view the UI screenshot


回答1:


Wrap the edit text with TextInputLayout with the desired android:hint attribute.

<com.google.android.material.textfield.TextInputLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content">
    
     <com.google.android.material.textfield.TextInputEditText
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:hint="StaffId"/>

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

check the official docs for more info and features




回答2:


@rcs I tried your method however it doesn't stay on top of the textValue from the start. I want it to always be there on the top of the textValue as soon as the application loads. Attached is the screenshot of how it looks like currently




回答3:


<android.support.design.widget.TextInputLayout
    android:id="@+id/inputlayout"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:theme="@style/loginActivityHintStyle">

    <EditText
        android:id="@+id/edittext"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="@string/the_hint_that_shows_with_the_editext"
        android:inputType="text"
        android:labelFor="@+id/edittext"
        android:maxLength="15"
        android:textColor="@drawable/login_activity_input_text_color" />

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


来源:https://stackoverflow.com/questions/65698423/how-to-group-together-a-label-and-a-textview-together-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!