“You need to use a Theme.AppCompat theme (or descendant) with the design library” error

霸气de小男生 提交于 2019-12-28 18:09:50

问题


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).

dependencies:

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'

layout:

<?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>

theme:

<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>

manifest:

<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);

回答1:


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);

The 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 design Views.



来源:https://stackoverflow.com/questions/36924481/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-the-design-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!