android - 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
会失去焦点。也没有出现边框。
解决方案
你需要检查几件事:
- 确保您
AppTheme
是Theme.MaterialComponents
(或后代)的孩子。
所以,你的 style.xml 应该是这样的
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents">
<!-- rest of your style items-->
</style>
</resources>
- 您在模块 gradle 文件中添加材料组件库
implementation 'com.google.android.material:material:1.2.0'
推荐阅读
- android - 通过一种方法获得多种类型的数组列表
- c# - 为什么选择 EF Database.SqlQuery
不能使用实体类型进行部分选择? - linux - Bash 虽然很慢
- android - Android Proguard:XmlPullParser 问题
- powershell - 如何使用 PowerShell 从 txt 文件中提取一些文本?
- jquery - $.get ajax 方法不适用于 laravel 中的州和城市
- c++ - while (cin >> *pchar) 等待 Ctrl-Z 之后的进一步输入
- reactjs - Redux-未从 mapStateToProps 获取状态
- date - Kotlin 仅获取明天的日期
- python - 未选中“详细信息右侧的摘要列”的 openpyxl 组列