如何在 xml 布局文件中使用自定义内联属性来自定义视图样式

How to use custom inline attributes in xml layout files for customizing style of views

自从我升级 Android 支持库后,我一直在努力为按钮、文本视图和其他视图设置自定义颜色。显然,内联属性被应用程序主题中定义的值覆盖。

我在values/styles中定义了一个应用主题。xml:

<resources>
    <!-- Base application theme. -->
    <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>
        <item name="colorControlActivated">@color/colorPrimary</item>
    </style>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

例如,在我的布局文件中,我将按钮的背景和文本颜色设置如下:

<Button
...
android:textColor="@color/colorPrimary"
android:background="@drawable/homebutton"
...
/>

下面是我在 colors.xml 中定义颜色的方式:

<resources>
    <color name="colorPrimary">#0066FF</color>
    <color name="colorPrimaryDark">#004AFF</color>
    <color name="colorAccent">#FFFFFF</color>
    <color name="background">#CCCCCC</color>
    <color name="text">#555555</color>
    <color name="green">#009900</color>
    <color name="red">#FF0000</color>
    <color name="orange">#FFA500</color>
    <color name="white">#FFFFFF</color>
    <color name="brown">#7A5230</color>
    <color name="goalkeeper">#FFA500</color>
    <color name="defender">#007c09</color>
    <color name="midfielder">#1d8dc0</color>
    <color name="attacker">#aa0a1c</color>
</resources>

此外,我在 Manifest 中正确设置了主题:

<application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:replace="android:theme">

在预览中一切看起来都不错,但是当我在模拟器中启动应用程序时,所有内联属性都被忽略并被我的应用程序主题定义的值覆盖。为什么这样?为什么忽略内联属性?有没有办法避免这种情况?

更新:我目前正在遵循此处指出的解决方案https://code.google.com/p/android/issues/detail?id=203136&q=-has%3Ablocked%20reportedby%3DDeveloper%20attachments%3D2%20label%3AComponent-Support-Libraries&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened这可能是由 Maven 本地存储库中的错误引起的问题

tools:replace="android:theme" 这个标签用于manifest merge 告诉如何处理特定的情况。

看起来它正在更改您最终产品清单中的主题属性。您可以尝试删除它吗?

更多信息您可以查看here

找到原因,解决问题:-)

显然,Maven 本地存储库(又名 m2repository)的版本 29 存在错误,正如此处所指出的 https://code.google.com/p/android/issues/detail?id=203136&q=-has%3Ablocked%20reportedby%3DDeveloper%20attachments%3D2%20label%3AComponent-Support-Libraries&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened and here https://code.google.com/p/android/issues/detail?id=203546

解决方法很简单:降级到26版本即可,这里可以下载http://dl.google.com/android/repository/android_m2repository_r26.zip