java - 无法使用自定义项目颜色设置服务布局样式

标签 java android android-styles android-attributes

我的应用程序有两个 Android 主题。我想使用 attrs.xml 创建一个新的自定义颜色值引用,但这会使应用程序不断崩溃。

虽然我看到其他答案说它与他们一起工作得很好,就像这个一样: How to add custom item in android Theme declaration?

这是我的 style.xml

    <resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="newValue">@color/black</item>
</style>


    <style name="Dark.AppTheme" parent="AppTheme">
        <item name="colorPrimary">#000000</item>
        <item name="colorPrimaryDark">#000000</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="newValue">@color/white</item>

    </style>
</resources>

attrs.xml

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="newValue" format="reference|color" />
</resources>

颜色.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#2196F3</color>
    <color name="colorPrimaryDark">#3F51B5</color>
    <color name="colorAccent">#FF9800</color>
    <color name="white">#FFFFFF</color>
    <color name="black">#000000</color>
</resources>

查看

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?newValue">
</RelativeLayout>

日志猫

java.lang.RuntimeException: Unable to create service com.moaness.servicetest.mainService: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class <unknown>
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3453)
        at android.app.ActivityThread.-wrap4(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1712)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6701)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:249)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)
     Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class <unknown>
     Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class <unknown>
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)

.
.
.

     Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValue{t=0x2/d=0x7f030140 a=-1}
        at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:946)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:930)
        at android.view.View.<init>(View.java:4738)
        at android.view.ViewGroup.<init>(ViewGroup.java:597)
        at android.widget.RelativeLayout.<init>(RelativeLayout.java:248)
        at android.widget.RelativeLayout.<init>(RelativeLayout.java:244)
        at android.widget.RelativeLayout.<init>(RelativeLayout.java:240)

这段代码有什么问题?

最佳答案

根本问题是这样的:

UnsupportedOperationException: Failed to resolve attribute...

每当您在布局膨胀期间看到这一点时,这意味着布局中引用的某些属性(或其中的某些可绘制元素、颜色等)在 LayoutInflater 用于实例化 ViewContext 主题中不可用。在本例中,Context 是一个Service,并且默认情况下,Service 没有附加主题。

要解决此问题,只需将 Service 包装在 ContextThemeWrapper 中,并使用所需主题的适当 R.style 值,并从中获取 LayoutInflater 。例如:

ContextThemeWrapper ctw = new ContextThemeWrapper(MyService.this, R.style.AppTheme);
LayoutInflater inflater = LayoutInflater.from(ctw);
View layout = inflater.inflate(R.layout.service_layout, null);
<小时/>

另一个可能的选择是自己在 Service 上手动设置主题,这样就不再需要 ContextThemeWrapper。例如:

@Override
public void onCreate() {
    super.onCreate();
    setTheme(R.style.AppTheme);
}

然后您可以直接从Service获取它们LayoutInflater;例如,

LayoutInflater inflater = LayoutInflater.from(MyService.this);
View layout = inflater.inflate(R.layout.service_layout, null);

理论上,这应该在任何地方都有效,但由于 Service 通常没有主题或管理​​ UI,因此考虑到自定义 Service 子类、制造商和自定义 ROM 修改等,我不确定它是否可以在每个环境中依赖。

事实上,我想说 ContextThemeWrapper 是更可靠的解决方案。

关于java - 无法使用自定义项目颜色设置服务布局样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57622360/

相关文章:

java - 如何隐藏java进程的参数?

java - 在 Java 中使用 JSch 列出 SFTP 服务器上目录的完整层次结构

android - android :progressBarStyle attribute in ProgressBar?是什么意思

android - 在 Android Studio 1.4.1 中设置 Android 工具栏背景和文本颜色

android - 如何更改 ActionBar 标题的背景颜色?

java - 在 JavaFX 8 中获取节点的可见状态

Java 扫描器令人头疼

android - 通过 Kotlin DSL 从 local.properties 读取值

多模块应用的 Android 测试覆盖率报告

android - 如何抑制约束布局错误 "MATCH_PARENT is not supported in ConstraintLayout"