如何解决TextInputLayout
我正在尝试使用Outline Boarder创建TextInputLayout。但是,无论何时我使用以下样式,它都会使应用程序发生故障(崩溃)。
在发布之前,我已经尝试了stackoverflow内外列出的所有解决方案,但是到目前为止,仍没有解决该问题的方法。
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
当前布局
<ScrollView...
<LinearLayout
android: layout_width = "200dp"
android: layout_height = "wrap_content"
android: orientation = "vertical"
android: layout_marginTop = "2dp" >
<com.google.android.material.textfield.TextInputLayout
android: id = "@+id/outlinedTextField"
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: hint = "@string/label"
style = "@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
app: boxStrokeWidthFocused = "2dp"
app: boxStrokeColor = "@color/border_primary" >
<com.google.android.material.textfield.TextInputEditText
android: layout_width = "match_parent"
android: layout_height = "wrap_content" />
</LinearLayout>
</ScrollView>
完整版式:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".user.userprofile.UserProfileFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/user_profile_header"/>
<LinearLayout
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/outlinedTextField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android: hint = "@string/label"
app:hintTextColor="@color/text_primary"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
app:boxStrokeWidthFocused="2dp"
app:hintTextColor="@color/text_primary"
app:boxStrokeColor="@color/border_primary">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
更多信息:添加style
时hint
失去了焦点。边框也不会出现。
解决方法
您需要检查几件事:
- 确保您的
result
是AppTheme
(或后代)。
因此,您的style.xml应该类似于
Theme.MaterialComponents
- 您将材料成分库添加到模块gradle文件中
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents">
<!-- rest of your style items-->
</style>
</resources>
TextInputLayout
不是自闭标签。
编辑您的代码,并将TextInputEditText
放在TextInputLayout
中。
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/outlinedTextField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android: hint = "@string/label"
app:hintTextColor="@color/text_primary"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
app:boxStrokeWidthFocused="2dp"
app:hintTextColor="@color/text_primary"
app:boxStrokeColor="@color/border_primary">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。