如何将com.google.android.material.textfield.TextInputEditText替换为TextView?
实现带边框和浮动标签的TextView
要把原来的输入框样式改成TextView版本,直接用TextInputLayout包裹TextView就能实现类似效果,调整以下属性即可匹配需求:
<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="32dp" android:layout_marginTop="16dp" android:layout_marginEnd="32dp" app:boxStrokeColor="@color/blue" android:hint="Full Name"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:fontFamily="@font/quicksand_bold" android:text="John Doe" <!-- 替换为你要显示的实际文本 --> android:textColor="@color/blue" android:paddingVertical="16dp" <!-- 让内容和边框的间距与原EditText对齐 --> android:paddingHorizontal="4dp" /> </com.google.android.material.textfield.TextInputLayout>
关键调整说明:
- 把原
EditText的hint移到TextInputLayout的android:hint属性上,标签会自动浮在边框上方,和输入框填完内容后的效果一致 - 给
TextView添加垂直内边距,保证视觉上和原输入框的内容间距统一 - 如果希望标签默认固定在上方、不需要动画,可以额外添加
app:hintAnimationEnabled="false"属性
内容的提问来源于stack exchange,提问作者Khan Mubashshir
相关产品推荐
相关产品推荐

