【问题标题】:Why cant I use TextInputLayout (from support design library) withour Theme.Appcompat为什么我不能在 Theme.Appcompat 中使用 TextInputLayout(来自支持设计库)
【发布时间】:2017-07-15 10:32:31
【问题描述】:

我使用 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 中使用了它。

但我的问题是为什么我不能将 Material 主题用于 Lollipop 之后,即当我在 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.access$800(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)

【问题讨论】:

  • 既然您已经支付了在项目中包含 AppCompat 的字节数,那么在所有 API 级别上使用 AppCompat 有什么问题?最重要的是,您不必担心 values-v21,因为您将在任何地方使用相同的代码。你认为你会得到什么? (没关系,AppCompat 不仅仅是关于 Action Bar 或 Material 设计,还包括自 Lollipop 到现在针对资源的许多错误修复和功能。)
  • 使用 Appcompat 没有问题,只是很好奇为什么在 Material design 支持的棒棒糖上没有它就无法工作

标签: android material-design android-textinputlayout android-support-design


【解决方案1】:

Design 库中的小部件依赖于从最新的 Android API 级别向后移植的功能。这包括对真实可绘制、颜色和属性分辨率的支持以及使用 AppCompat 版本的平台属性(例如colorAccentcolorControlNormalcolorError 等)。

如果您的应用已经将 AppCompat 用于 API 21 以下的平台,那么您可以安全地使用以上 API 21 的 AppCompat,包括 API 21。不仅如此,我鼓励你这样做。它包含错误修正和从更新的 API 级别向后移植的功能。

不要让自己头疼,不要尝试同时使用 AppCompat 和 Material 主题,除非您确切知道自己在做什么。

如果您的应用是使用 minSdkVersion 21 设计的,并且您决定绝对不想使用 AppCompat,则可以使用仅使用框架功能的 Design 库的分支。

https://github.com/commonsguy/cwac-crossport

它仍然使用support-v4 库的部分内容(完全没问题):

这个项目依赖于support-annotations和前support-v4的三个部分:support-compatsupport-core-uisupport-core-utils。所有这些都将通过传递依赖项引入,并且您可能已经在使用这些依赖项中的部分或全部。

【讨论】:

  • 这看起来是最接近我的查询的答案。
  • 如果我只想将 Material.Light.NoActionBar.TranslucentDecor 用于 API>21 仅用于一项活动?
  • @Zhigalin 半透明装饰意味着&lt;item name="windowTranslucentStatus"&gt;true&lt;/item&gt;&lt;item name="windowTranslucentNavigation"&gt;true&lt;/item&gt;。您可以在项目的子主题中执行此操作。
  • @EugenPechanec 如果我使用它,编译失败并出现错误:Error:error: style attribute 'attr/windowTranslucentStatus (aka com.phr.app:attr/windowTranslucentStatus)' not found.Error:error: style attribute 'attr/windowTranslucentNavigation (aka com.phr.app:attr/windowTranslucentNavigation)' not found.
  • @zhigalin 缺少 android: 前缀
【解决方案2】:

将此样式用于 TextInputLayout:

  <style name="MyTheme" parent="Widget.Design.TextInputLayout">

【讨论】:

    猜你喜欢
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    相关资源
    最近更新 更多