"You need to use a Theme.AppCompat theme (or descendant) with the design library" 错误
"You need to use a Theme.AppCompat theme (or descendant) with the design library" error
我每次都收到 "You need to use a Theme.AppCompat theme (or descendant) with the design library" 错误,即使我显然使用的是 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>
我在 service:
中扩充布局
tooltipContainer = (CoordinatorLayout) LayoutInflater.from(this).inflate(R.layout.tooltip_layout, null);
创建一个 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
有适当的主题来扩充图书馆 View
s.
或者,如果在布局 XML 中处理它会更合适或更少涉及,您可以在根 <ViewGroup>
上设置一个 android:theme
属性,这只会导致LayoutInflater
在内部进行 Context
换行。例如:
<android.support.design.widget.CoordinatorLayout
...
android:theme="@style/TranslucentAppTheme">
但是,这仅适用于从 Lollipop(API 级别 21)开始的 LayoutInflater
平台。 support/androidx 库能够在旧版本上处理该属性,但它的设置方式仅适用于 Activity
类,并且自己进行包装可能更简单在那种情况下。
在logcat中也收到这样的错误:
"E/ThemeUtils: View class
TableCircleCustomView is
an AppCompat widget that can only be used with a Theme.AppCompat theme
(or descendant)."
我的应用主题是“Theme.AppCompat.Light.NoActionBar”
这是我的自定义视图 class:
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);
}
谁能帮忙解决一下。不知道我应该设置什么元素以及如何设置主题 ((
如果您从服务中收到错误,我认为 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>
请注意,我没有 AppTheme.NoActionBar 来自 Theme.AppCompat 主题。
我每次都收到 "You need to use a Theme.AppCompat theme (or descendant) with the design library" 错误,即使我显然使用的是 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>
我在 service:
中扩充布局tooltipContainer = (CoordinatorLayout) LayoutInflater.from(this).inflate(R.layout.tooltip_layout, null);
创建一个 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
有适当的主题来扩充图书馆 View
s.
或者,如果在布局 XML 中处理它会更合适或更少涉及,您可以在根 <ViewGroup>
上设置一个 android:theme
属性,这只会导致LayoutInflater
在内部进行 Context
换行。例如:
<android.support.design.widget.CoordinatorLayout
...
android:theme="@style/TranslucentAppTheme">
但是,这仅适用于从 Lollipop(API 级别 21)开始的 LayoutInflater
平台。 support/androidx 库能够在旧版本上处理该属性,但它的设置方式仅适用于 Activity
类,并且自己进行包装可能更简单在那种情况下。
在logcat中也收到这样的错误:
"E/ThemeUtils: View class TableCircleCustomView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant)."
我的应用主题是“Theme.AppCompat.Light.NoActionBar”
这是我的自定义视图 class:
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);
}
谁能帮忙解决一下。不知道我应该设置什么元素以及如何设置主题 ((
如果您从服务中收到错误,我认为 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>
请注意,我没有 AppTheme.NoActionBar 来自 Theme.AppCompat 主题。