android - 如何在 Android Studio 中做浮动提示/文本?
问题描述
I have a box to type in the user's username, and when the area is selected, I want the "username" text to slide up so it is still visible when typing the username. 有关更多信息,请参阅代码。
<ImageView
android:id="@+id/imageView8"
android:layout_width="332dp"
android:layout_height="45dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView7"
app:srcCompat="@drawable/input_background" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:fontFamily="@font/days_one"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Username"
android:textColor="#747474"
app:layout_constraintBottom_toBottomOf="@+id/imageView8"
app:layout_constraintStart_toStartOf="@+id/imageView8"
app:layout_constraintTop_toTopOf="@+id/imageView8" />
</androidx.constraintlayout.widget.ConstraintLayout>
解决方案
您可以使用 TextInputLayout 和其中的 editText 来实现这一点
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/username_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/etUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:hint="Username" />
</com.google.android.material.textfield.TextInputLayout>
检查标题为“显示浮动标签反馈”的此链接
https://guides.codepath.com/android/Working-with-the-EditText#displaying-floating-label-feedback
推荐阅读
- python - 调整主窗口中的框架大小以适应窗口
- git - 如何在两个 git 分支中合并单个目录?
- php - Laravel 商店数据库
- django - 禁用多选 Django 小部件中的选择
- reactjs - 如何通过单击同级组件在反应中显示/隐藏组件
- javascript - 是否需要打开谷歌表才能运行功能?
- python - 通过 Okta 身份验证使用 Databricks Snowflake 连接器连接到 Snowflake
- javascript - 如何为客户端可视化的快速排序算法添加延迟间隔?
- meteor - Meteor 和 withTracker:为什么一个组件会渲染两次?
- spring-boot - Spring Boot Multi Module Gradle Project 类路径问题:Package Not Found, Symbol not Found