【问题标题】:"You need to use a Theme.AppCompat theme (or descendant) with the design library" error“您需要将 Theme.AppCompat 主题(或后代)与设计库一起使用”错误
【发布时间】:2016-08-23 19:07:41
【问题描述】:

我每次都收到“您需要在设计库中使用 Theme.AppCompat 主题(或后代)”错误,即使我显然使用的是 AppCompat 主题(后代主题)。

依赖关系:

compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.android.support:support-v4:23.3.0'

布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/tooltip_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone">

    <ImageView
        android:id="@+id/tooltip_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_delete_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/tooltip_image"
        app:layout_anchorGravity="top|end"/>

</android.support.design.widget.CoordinatorLayout>

主题:

<style name="TranslucentAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

清单:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/TranslucentAppTheme">
        <activity android:name=".MainActivity">
            (...)
</activity>

我在 服务 中扩充布局:

tooltipContainer = (CoordinatorLayout) LayoutInflater.from(this).inflate(R.layout.tooltip_layout, null);

【问题讨论】:

    标签: android layout styles android-appcompat


    【解决方案1】:

    创建一个ContextThemeWrapper 以使用您的自定义主题包装Service 的Context,并从中获取LayoutInflater。

    ContextThemeWrapper ctx = new ContextThemeWrapper(this, R.style.TranslucentAppTheme);
    tooltipContainer = (CoordinatorLayout) LayoutInflater.from(ctx)
        .inflate(R.layout.tooltip_layout, null);
    

    ContextThemeWrapper 将给定的Context 的主题修改为您在构造函数中指定的主题。由于Service 并没有真正的主题,它只是将你的主题附加到Service 的Context 上,然后LayoutInflater 具有适当的主题来扩展库Views。


    或者,如果在布局 XML 中处理它更合适或更不涉及,您可以在根 &lt;ViewGroup&gt; 上设置一个 android:theme 属性,这只会导致 LayoutInflater 执行 Context内部包裹。例如:

    <android.support.design.widget.CoordinatorLayout
        ...
        android:theme="@style/TranslucentAppTheme">
    

    但是,这仅适用于从 Lollipop 开始的平台 LayoutInflater(API 级别 21)。 support/androidx 库能够在旧版本上处理该属性,但它的设置方式仅适用于 Activity 类,在这种情况下自己进行包装可能更简单。

    【讨论】:

      【解决方案2】:

      如果您从服务中得到错误,我认为 Mike M 解决方案是正确的解决方案,这就是原始问题的陈述方式。我认为 Yevhen 是在谈论解决这个不是源自服务的问题。针对这种情况,我会分享我的解决方案。

      我遇到了同样的错误,指出我需要使用 Theme.AppCompat 主题。在我的例子中,它来自一个包含 6 个 NumberPicker 小部件的自定义对话框。每当我打开该对话框时,该错误将发生 6 次(每个小部件一个)。这是我的样式最初的样子:

      <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
          <!-- Customize your theme here. -->
          <item name="colorPrimary">@color/colorPrimary</item>
          <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
          <item name="colorAccent">@color/colorAccent</item>
      </style>
      
      <style name="AppTheme.NoActionBar">
          <item name="windowActionBar">false</item>
          <item name="windowNoTitle">true</item>
      </style>
      

      而且,这是我为纠正问题所做的:

      <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
          <!-- Customize your theme here. -->
          <item name="colorPrimary">@color/colorPrimary</item>
          <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
          <item name="colorAccent">@color/colorAccent</item>
      </style>
      
      <style name="AppTheme.NoActionBar" parent="AppTheme">
          <item name="windowActionBar">false</item>
          <item name="windowNoTitle">true</item>
      </style>
      

      请注意,我没有派生自 Theme.AppCompat 主题的 AppTheme.NoActionBar。

      【讨论】:

        【解决方案3】:

        在logcat中也收到这样的错误:

        "E/ThemeUtils:查看类 TableCircleCustomView 是 只能与 Theme.AppCompat 主题一起使用的 AppCompat 小部件 (或后代)。”

        我的应用主题是“Theme.AppCompat.Light.NoActionBar”

        这是我的自定义视图类:

        class TableCircleCustomView @JvmOverloads constructor(
            context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
        ) : AppCompatTextView(context, attrs, defStyleAttr) {
            ....
        }
        

        然后在 Fragment 中,我将这个对象添加到 RelativeLayout,就像这样

        val viewItem = TableCircleCustomView(context)     
        with(RelativeLayout.LayoutParams(objectWidth, objectHeight)) {
          leftMargin = objectPosX
          topMargin = objectPosY
            
          binding.restaurantMap.addView(viewItem, this);
        }
        

        谁能帮忙解决。不知道我应该设置什么元素以及如何设置主题((

        【讨论】:

        • 发布一个新问题,而不是一个问题的答案;-)
        猜你喜欢
        • 2016-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多