在尝试使用Android的新TextInputField
并想要分享我的解决方案时发生了崩溃。
在android appcompat库中尝试新的TextInputField会导致我的应用程序崩溃。这是我的布局xml。
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="e-mail"
android:inputType="textEmailAddress"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
我得到的错误:
android.view.InflateException: Binary XML file line #20: Error inflating class android.support.design.widget.TextInputLayout.
解决方案:将hintTextAppearance
属性添加到TextInputLayout
,因此lead标签如下所示:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@android:style/TextAppearance.Medium">
确保gradle文件中包含以下依赖项:
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
工作范例:
<android.support.design.widget.TextInputLayout
android:id="@+id/txtEmail_InpLyt"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/txtEmail"
android:hint="Email Address"
android:singleLine="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</android.support.design.widget.TextInputLayout>
(不需要设置hintTextAppearance
。)
更新:
如果您遇到问题文本没有出现在较新版本的Android(Marshmallow / Nougat)中,请将库更新到版本22.2.1(请参阅TextInputLayout not showing EditText hint before user focus on it)。
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:design:22.2.1'
我在Android Studio 3.0.1中遇到了同样的问题
您只需将以下行添加到gradle.properties(Project Propeties)中:
android.enableAapt2 = FALSE
对我来说,工作从EditText更改为TextInputEditText
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/date_raffle"/>
</android.support.design.widget.TextInputLayout>
这也发生在我身上,我提出了一个不需要更改App主题的解决方案,只是改变了TextInputLayout的主题:
<android.support.design.widget.TextInputLayout
android:id="@+id/testingInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.AppCompat">
<EditText
android:id="@+id/testingEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/testText"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
如果您还没有,则需要添加appCompat库:
compile 'com.android.support:appcompat-v7:23.0.1'
在扩展包含TextInputLayout
的XML时遇到了同样的问题。通过在我的应用程序上设置正确的样式来解决此问题。就像它在这里说的那样:android design support library
我有以下问题
Caused by: android.view.InflateException: Binary XML file line #38: Error inflating class android.support.design.widget.TextInputLayout
我的style.xml是
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="android:Theme.Material">
<!-- Customize your theme here. -->
</style>
</resources>
正如它在this post on Design Support Library所说的那样
请注意,由于Design库依赖于Support v4和AppCompat支持库,因此在添加Design库依赖项时将自动包含这些库。
因此无需在gradle文件中添加以下行
compile 'com.android.support:appcompat-v7:22.2.0'
我发现链接可以解释设计支持库是AppCompat的一部分,它需要AppCompat Theme库才能工作。所以我修改了我的style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
它奏效了。
使用以下内容:
View view = LayoutInflator.from(getActivity()).inflate(R.layout.your_layout_file,parent,false);
确保从AppCompatActivity创建LayoutInflater
恩。
代替
inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
使用:
inflater = LayoutInflater.from(<appCompatActivity>);
这个解决方案都不适合我。我在用
compile 'com.android.support:appcompat-v7:28.0.0'
compile 'com.android.support:design:28.0.0'
试过所有的解决方案。我的应用程序在具有较低sdk的设备中运行完美。但是在较新的(大于sdk 26)设备中崩溃。但经过2天的搔痒终于得到了解决方案。
我的布局编辑器显示错误
The following classes could not be instantiated:
- android.support.design.widget.TextInputLayout
但它并没有给我足够的信息。直到我在布局编辑器中查看其他错误。在渲染问题下我发现了
Couldn't resolve resource @string/path_password_strike_through
在调查此渲染问题后,我终于得到了解决方案。您需要做的就是添加
app:passwordToggleDrawable="@string/string_name"
在TextInputLayout xml文件中。
<android.support.design.widget.TextInputLayout
android:id="@+id/login_input_layout_password"
android:layout_width="match_parent"
app:passwordToggleDrawable="@drawable/ic_lock_outline_grey600_24dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:backgroundTint="@color/colorAccent"
app:boxStrokeColor="#555"
android:textColorHint="@color/colorPrimary">
<EditText
android:id="@+id/login_input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.design.widget.TextInputLayout>
为了明确Android Studio 3.*
,现在改为
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
(如果在添加它时变成红色,请接受Android Studio想要建议的版本)
这需要在里面
dependencies {}
build.gradle (Module:app)
的部分所以它看起来像这样
dependencies {
...
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
}
然后击中sync now
在adroidx它为我工作我有图书馆
implementation 'com.android.support:appcompat-v7:28.0.0'
//design widget
implementation 'com.android.support:design:28.0.0'
我改变
<android.support.design.widget.TextInputLayout
对于
<com.google.android.material.textfield.TextInputLayout
你可能已经添加了它compile 'com.android.support:appcompat-v7:22.2.0'
但你需要添加compile 'com.android.support:design:22.2.0'
你可以尝试这种依赖:
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
在你的gradle文件中。