“您需要将 Theme.AppCompat 主题(或后代)与设计库一起使用"错误 [英] "You need to use a Theme.AppCompat theme (or descendant) with the design library" error

查看:32
本文介绍了“您需要将 Theme.AppCompat 主题(或后代)与设计库一起使用"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm getting "You need to use a Theme.AppCompat theme (or descendant) with the design library" error every time even if I'm obviously using an AppCompat Theme (a descendant one).

依赖项:

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>

我在一个服务中夸大了布局:

I'm inflating the layout inside a service:

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

推荐答案

创建一个 ContextThemeWrapper 来包装 ServiceContext自定义主题,并从中获取 LayoutInflater.

Create a ContextThemeWrapper to wrap the Service's Context with your custom theme, and get the LayoutInflater from that.

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

ContextThemeWrapper 修改给定的Context 的主题与您在构造函数中指定的主题.由于 Service 并没有真正的主题,它只是将您的主题添加到 ServiceContext,然后是 LayoutInflater 具有适当的主题来扩充库 Views.

ContextThemeWrapper modifies the given Context's theme with the one you specify in the constructor. Since a Service doesn't really have a theme, it just tacks yours onto the Service's Context, then the LayoutInflater has the appropriate theme to inflate the library Views.

或者,如果在布局 XML 中处理它更合适或更少参与,您可以在根 上设置一个 android:theme 属性>,这只会导致 LayoutInflater 在内部进行 Context 包装.例如:

Alternatively, if handling it in the layout XML would be more appropriate or less involved, you might be able set an android:theme attribute on the root <ViewGroup>, which simply causes the LayoutInflater to do the Context wrapping internally. For example:

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

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

However, this will only work with the platform LayoutInflater starting with Lollipop (API level 21). The support/androidx libraries are able to handle that attribute on older versions, but the way it's set up is intended for use in Activity classes only, and it's likely simpler to just do the wrapping yourself in that case.

这篇关于“您需要将 Theme.AppCompat 主题(或后代)与设计库一起使用"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆