TextInputLayout 的轮廓文本字段

Roo*_*ter 5 android android-layout android-studio

我正在尝试创建一个带有大纲边框的 TextInputLayout。但每当我使用以下样式时,应用程序就会发生故障(崩溃)。
在发布之前,我已经尝试了 stackoverflow 内外列出的所有解决方案,但到目前为止还没有解决问题。

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
Run Code Online (Sandbox Code Playgroud)

目前布局

<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>
Run Code Online (Sandbox Code Playgroud)

完整布局:

    <?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>
Run Code Online (Sandbox Code Playgroud)

更多信息:style添加时hint会失去焦点。边框也不会出现。

Zai*_*ain 5

您需要检查几件事:

  1. 确保您是(或后代)AppTheme的孩子 。Theme.MaterialComponents

所以,你的 style.xml 应该是这样的

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents">
         <!-- rest of your style items-->
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)
  1. 您在模块 gradle 文件中添加材质组件库

implementation 'com.google.android.material:material:1.2.0'

  • 有效!!!两位,真的很谢谢你们!有了这个,Zain 你解决了我最痛苦的问题,Dev,你解决了另一个问题。感谢您的帮助。 (2认同)