Android解析样式的属性和属性? [英] Android Resolving style properties and attributes?

查看:98
本文介绍了Android解析样式的属性和属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Android应用程序有一个主项目模块,在样式文件中具有以下声明.此主题用于应用程序"标签上的清单文件中,因此应用程序"元素内的所有组件都应用了相同的主题.

My Android Application has a main Project Module that has the following declaration in the styles file. This Theme is used in the Manifest file on the "application" tag so all components within the "application" element have the same theme applied.

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:colorPrimary" tools:ignore="NewApi">
        @color/light_black
    </item>
    <item name="colorPrimary">@color/light_black</item>
    <item name="android:colorAccent" tools:ignore="NewApi">@color/sapphire</item>
    <item name="colorAccent">@color/sapphire</item>
    <item name="android:colorBackground">@color/primary_background</item>
    <item name="android:textColorPrimary">@color/title_color</item>
    <item name="android:colorButtonNormal" tools:ignore="NewApi">@color/sapphire</item>
    <item name="colorButtonNormal">@color/sapphire</item>
    <item name="android:colorForeground" tools:ignore="NewApi">
        @color/title_color
    </item>
    <item name="android:titleTextStyle">@style/toolbar_title</item>
    <item name="android:navigationIcon" tools:ignore="NewApi">
        ?android:attr/homeAsUpIndicator</item>
    <item name="navigationIcon">?android:attr/homeAsUpIndicator</item>
    <item name="android:colorControlNormal" tools:ignore="NewApi">@android:color/white</item>
    <item name="colorControlNormal">@android:color/white</item>
</style>

我在主项目模块旁边还有一个库模块,在这里我将放置最常用的视图,小部件和基本组件,这些视图,小组件和基本组件可与我的应用程序项目中的其他应用程序或组织中的其他应用程序一起重用.至于Gradle依赖项声明,当然,项目模块取决于库模块,而不是库模块.

I also have a Library module next to the main Project module, where I will be putting most commonly used Views, Widgets and base-components that can be reused with other applications within my Application Project, or others within the organization. As for Gradle Dependency declaration, the Project Module depends on the Library Module, and not the vice-versa, of course.

如何在运行时根据主项目模块中相应上下文(活动实例)的主题,在图书馆模块"组件代码库中解析"colorAccent"和"colorControlNormal"默认的android属性?

How do I resolve "colorAccent" and "colorControlNormal" default android-attributes inside my Library Module components code-base at run-time depending on the Theme of the appropriate Context (Activity instance) in the main Project Module?

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools">
<item android:color="?android:attr/colorControlNormal"
    android:state_activated="false"
    tools:ignore="NewApi"/>

<item android:color="?android:attr/colorAccent"
    android:state_activated="true"
    tools:ignore="NewApi"/>
</selector>

推荐答案

所以这是我解决此问题的方法.

So here's how I fixed this.

attrs.xml.请注意,格式是整数",尽管属性可能是颜色.

attrs.xml in the Library Module. Notice the format is a "integer", although the attributes are supposedly colors.

<attr name="progressColorControlNormal" format="reference|integer" />
<attr name="progressColorAccent" format="reference|integer" />

由于主项目模块依赖于库模块,因此我可以在styles.xml文件的主题定义中访问这些属性.

Because the main Project Module is dependent on the Library Module, I could access these attributes in the Theme definition in the styles.xml file.

<item name="progressColorControlNormal">@android:color/white</item>
<item name="progressColorAccent">@color/sapphire</item>

请注意颜色的解析值,而不是主题本身的引用,例如?android:attr/colorControlNormal"和?android:attr/colorAccent"或其变体.

Do notice the resolved values for the colors, and not references from the Theme itself such as "?android:attr/colorControlNormal" and "?android:attr/colorAccent", or their variants.

库模块"中的可绘制xml现在会自动解析主题色.

The drawable xml in the Library Module now resolves the Themed Colors automatically.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools">
    <item android:color="?progressColorControlNormal"
        android:state_activated="false"
        tools:ignore="NewApi"/>

    <item android:color="?progressColorAccent"
        android:state_activated="true"
        tools:ignore="NewApi"/>
</selector>

这篇关于Android解析样式的属性和属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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