首页 > 解决方案 > 自定义属性在自定义主题中不起作用

问题描述

考虑到以下自定义属性,我创建了一个自定义主题:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="GlideColour" format="color"/>
    <attr name="BackgroundColour" format="reference|color"/>
    <attr name="SuggestionColour" format="color"/>
    <attr name="KeyBackgroundColour" format="color"/>
    <attr name="KeyBorderColour" format="color"/>
    <attr name="SpaceBackgroundColour" format="color"/>
    <attr name="SpaceTextColour" format="color"/>
    <attr name="TextColour" format="color"/>
    <attr name="BackEnterColour" format="color"/>
    <attr name="PopUpDrawerColour" format="color"/>
    <attr name="DrawerSelectColour" format="color"/>
    <attr name="UtilityTextColour" format="color"/>
    <attr name="KeyboardBackground" format="reference"/>
    <attr name="KeyStyle" format="reference"/>
    <attr name="SpaceStyle" format="reference"/>
    <attr name="UtilityStyle" format="reference"/>
    <attr name="EnterStyle" format="reference"/>
    <attr name="BackStyle" format="reference"/>
    <attr name="KeyboardStyle" format="reference"/>
    <attr name="CapsStyle" format="reference"/>
    <attr name="DrawerButtonStyle" format="reference"/>
</resources>

这是我制作的主题:

<style name="Standard" 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="KeyboardBackground">@color/standard_background_color</item>
         <item name="GlideColour">@color/standard_glide_color</item>
         <item name="BackgroundColour">@color/standard_background_color</item>
         <item name="SuggestionColour">@color/standard_suggestion_color</item>
         <item name="SpaceBackgroundColour">@color/standard_space_background_color</item>
         <item name="SpaceTextColour">@color/standard_space_text_color</item>
         <item name="TextColour">@color/standard_text_color</item>
         <item name="BackEnterColour">@color/standard_back_space_color</item>
         <item name="UtilityTextColour">@color/standard_utility_color</item>
         <item name="KeyStyle">@style/standard_key_button</item>
         <item name="SpaceStyle">@style/standard_space_button</item>
         <item name="UtilityStyle">@style/standard_utility_style</item>
         <item name="EnterStyle">@style/standard_enter_style</item>
         <item name="BackStyle">@style/standard_back_style</item>
         <item name="KeyboardStyle">@style/standard_keyboard_style</item>
         <item name="CapsStyle">@style/standard_caps_style</item>
         <item name="DrawerButtonStyle">@style/standard_drawer_button_style</item>
     </style>

但是当从这个主题应用自定义样式属性时,它不会被添加到视图中,例如:

<com.nisarg.nboard.SwipeLayout android:layout_width="match_parent"
    android:layout_height="@dimen/keyboard_height"
    style="?KeyboardStyle"
    android:id="@+id/Rel"
    xmlns:android="http://schemas.android.com/apk/res/android">

给我这个: 在此处输入图像描述

它应该在哪里给我这个:

在此处输入图像描述

那就是根本没有将引用的样式添加到元素中。我不知道为什么这不起作用。这里有什么问题?

这是引用的样式

<style name="standard_keyboard_style" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:background">@color/standard_background_color</item>
    </style>

我发现的问题是在引用中,我不能使用“attr/”,否则它会引发错误。

标签: javaandroidandroid-themeandroid-stylesandroid-attributes

解决方案


尝试将您的属性声明为可样式化

像这样:

<declare-styleable name="SwipeLayout">
    <attr name="GlideColour" format="color"/>
    <!-- other attributes go here-->
</declare-styleable>

更新

您可以为多个视图使用相同的自定义属性

像这样:

<resources>
    <attr name="GlideColour" format="color"/>
    <!-- other attributes go here-->
    
    <declare-styleable name="SwipeLayout">
        <attr name="GlideColour" />
    </declare-styleable>

    <declare-styleable name="SomeOtherView">
        <attr name="GlideColour" />
    </declare-styleable>
    
    <declare-styleable name="ThirdCustomView">
        <attr name="GlideColour" />
    </declare-styleable>

</resources>

推荐阅读