【问题标题】:Custom Attributes not Working in a Custom theme自定义属性在自定义主题中不起作用
【发布时间】:2020-10-22 20:44:53
【问题描述】:

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

<?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/ 否则会引发错误。

【问题讨论】:

    标签: java android android-theme android-styles android-attributes


    【解决方案1】:

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

    像这样:

    <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>
    

    【讨论】:

    • 我会这样做,但是我需要对我使用的每个其他视图进行子类化,因为我想要为我使用的每个视图自定义属性
    • 在这种情况下,您可以对多个视图使用相同的属性。查看更新的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 2011-10-15
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多