【问题标题】:Outlined Text Field for TextInputLayoutTextInputLayout 的概述文本字段
【发布时间】:2020-11-30 06:58:57
【问题描述】:

我正在尝试使用 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 失去焦点。也没有出现边框。

【问题讨论】:

  • 嘿@Rootster,我不知道您所说的故障具体是什么意思,但是TextInputEditText 是在TextInputEditText 内部使用的。请粘贴您的完整 xml 以及您的故障到底是什么。也许我可以帮忙
  • 确实有必要知道确切的“故障”是什么。而@ravi 的意思是TextInputEditText 必须在TextInputLayout 里面(他打了一个小错字)
  • 每次启动时应用程序崩溃的故障。但是当我删除style = "@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"它工作正常。另外我想补充一下,当我使用上述样式时,“提示”也会失焦。

标签: android android-studio android-layout


【解决方案1】:

您需要检查几件事:

  1. 确保您的 AppThemeTheme.MaterialComponents(或后代)。

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

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

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

【讨论】:

  • 当前gradle版本implementation 'com.google.android.material:material:1.1.0'
  • @Rooster 你用的版本没问题,只要确保你添加它就可以了
  • 我已经检查了我的 AppTheme 的风格。我有这样的东西。 &lt;style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"&gt; 你能告诉我它应该是什么样子Theme.MaterialComponents
  • 您的AppTheme's parent 设置为AppCompat。由于您使用的是Material Components,请将父级设置为Theme.MaterialComponents.Light.DarkActionBar
  • 成功了!!!你们两位,真的谢谢你们!有了这个,Zain 你解决了我最痛苦的问题,而 Dev,你解决了另一个问题。感谢您的帮助。
【解决方案2】:

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>

【讨论】:

  • @Hi Dev 这是对的,但发帖人已经补充说,当他们删除 style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox".. 我认为这只是他们帖子中的一个错字
  • 是的,这是一个错字。对于那个很抱歉。它不在实际代码中。我会在问题中解决它
  • @Rooster 让你的AppTheme's 父级设置为Material Theme,就像@Zain 说的那样。
猜你喜欢
  • 2019-02-06
  • 2021-12-16
  • 2023-03-17
  • 1970-01-01
  • 2020-10-05
  • 2019-12-18
  • 1970-01-01
  • 1970-01-01
  • 2018-05-22
相关资源
最近更新 更多