为什么我不能在没有 Theme.Appcompat 的情况下使用 TextInputLayout(来自支持设计库)
Why cant I use TextInputLayout (from support design library) withour Theme.Appcompat
我正在使用如下的 TextInputLayout
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_layout_name"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edit_text_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_full_name"
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
如果我的应用程序使用 Theme.AppCompat
,代码可以正常工作
Theme.AppCompat 对于下面的 Lollipop 是强制性的是有效的,因此我在 values\theme.xml.
中使用它
但我的问题是为什么我不能在 Lollipop 之后使用 Material 主题,即当我在 values-v21\theme.xml 中使用 Theme.Material 时应用程序崩溃。 ...
关于这个问题有什么线索吗?
<style name="MyTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
下方应用因 RuntimeException 而崩溃
在膨胀时我得到他的二进制膨胀错误 XML
Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 18
at android.content.res.TypedArray.getColor(TypedArray.java:401)
at android.support.design.widget.CollapsingTextHelper.setCollapsedTextAppearance(CollapsingTextHelper.java:166)
at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:106)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:381)
at android.app.Activity.setContentView(Activity.java:2144)
at com.example.trybindinglib.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2407)
at android.app.ActivityThread.access0(ActivityThread.java:149)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5321)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
将此样式用于 TextInputLayout:
<style name="MyTheme" parent="Widget.Design.TextInputLayout">
Design 库中的小部件依赖于从 Android 的最新 API 级别向后移植的功能。这包括支持真实的可绘制对象、颜色和属性解析以及使用 AppCompat 版本的平台属性(例如 colorAccent
、colorControlNormal
、colorError
等)。
如果您的应用程序已经在 API 21 以下的平台上使用 AppCompat,您可以安全地使用上面的 AppCompat,包括 API 21。不仅如此,我鼓励你这样做。它包含从更新的 API 级别向后移植的错误修复和功能。
不要让自己头疼,不要尝试同时使用 AppCompat 和 Material 主题,除非您确切地知道自己在做什么。
如果您的应用程序是使用 minSdkVersion 21 设计的,并且您决定绝对不想使用 AppCompat,则有一个仅使用框架功能的设计库分支。
https://github.com/commonsguy/cwac-crossport
它仍然使用部分 support-v4
库(完全没问题):
This project depends upon support-annotations
and three pieces of the former support-v4
: support-compat
, support-core-ui
, and support-core-utils
. All will be pulled in via transitive dependencies, and you probably already are using some or all of those dependencies anyway.
我正在使用如下的 TextInputLayout
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_layout_name"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edit_text_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_full_name"
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
如果我的应用程序使用 Theme.AppCompat
,代码可以正常工作Theme.AppCompat 对于下面的 Lollipop 是强制性的是有效的,因此我在 values\theme.xml.
中使用它但我的问题是为什么我不能在 Lollipop 之后使用 Material 主题,即当我在 values-v21\theme.xml 中使用 Theme.Material 时应用程序崩溃。 ... 关于这个问题有什么线索吗?
<style name="MyTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
下方应用因 RuntimeException 而崩溃 在膨胀时我得到他的二进制膨胀错误 XML
Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 18
at android.content.res.TypedArray.getColor(TypedArray.java:401)
at android.support.design.widget.CollapsingTextHelper.setCollapsedTextAppearance(CollapsingTextHelper.java:166)
at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:106)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:381)
at android.app.Activity.setContentView(Activity.java:2144)
at com.example.trybindinglib.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2407)
at android.app.ActivityThread.access0(ActivityThread.java:149)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5321)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
将此样式用于 TextInputLayout:
<style name="MyTheme" parent="Widget.Design.TextInputLayout">
Design 库中的小部件依赖于从 Android 的最新 API 级别向后移植的功能。这包括支持真实的可绘制对象、颜色和属性解析以及使用 AppCompat 版本的平台属性(例如 colorAccent
、colorControlNormal
、colorError
等)。
如果您的应用程序已经在 API 21 以下的平台上使用 AppCompat,您可以安全地使用上面的 AppCompat,包括 API 21。不仅如此,我鼓励你这样做。它包含从更新的 API 级别向后移植的错误修复和功能。
不要让自己头疼,不要尝试同时使用 AppCompat 和 Material 主题,除非您确切地知道自己在做什么。
如果您的应用程序是使用 minSdkVersion 21 设计的,并且您决定绝对不想使用 AppCompat,则有一个仅使用框架功能的设计库分支。
https://github.com/commonsguy/cwac-crossport
它仍然使用部分 support-v4
库(完全没问题):
This project depends upon
support-annotations
and three pieces of the formersupport-v4
:support-compat
,support-core-ui
, andsupport-core-utils
. All will be pulled in via transitive dependencies, and you probably already are using some or all of those dependencies anyway.