【问题标题】:How to: Define theme (style) item for custom widget如何:为自定义小部件定义主题(样式)项
【发布时间】:2011-05-28 11:52:18
【问题描述】:

我为我们在整个应用程序中广泛使用的控件编写了一个自定义小部件。小部件类派生自ImageButton,并以几种简单的方式对其进行了扩展。我已经定义了一种样式,可以在使用时应用到小部件,但我更愿意通过主题进行设置。在R.styleable 中,我看到了像imageButtonStyletextViewStyle 这样的小部件样式属性。有没有办法为我写的自定义小部件创建类似的东西?

【问题讨论】:

    标签: android android-theme


    【解决方案1】:

    是的,有一种方法:

    假设您有一个小部件的属性声明(在attrs.xml 中):

    <declare-styleable name="CustomImageButton">
        <attr name="customAttr" format="string"/>
    </declare-styleable>
    

    声明一个您将用于样式引用的属性(在attrs.xml 中):

    <declare-styleable name="CustomTheme">
        <attr name="customImageButtonStyle" format="reference"/>
    </declare-styleable>
    

    为小部件声明一组默认属性值(在styles.xml中):

    <style name="Widget.ImageButton.Custom" parent="android:style/Widget.ImageButton">
        <item name="customAttr">some value</item>
    </style>
    

    声明一个自定义主题(themes.xml):

    <style name="Theme.Custom" parent="@android:style/Theme">
        <item name="customImageButtonStyle">@style/Widget.ImageButton.Custom</item>
    </style>
    

    将此属性用作小部件构造函数中的第三个参数(在CustomImageButton.java 中):

    public class CustomImageButton extends ImageButton {
        private String customAttr;
    
        public CustomImageButton( Context context ) {
            this( context, null );
        }
    
        public CustomImageButton( Context context, AttributeSet attrs ) {
            this( context, attrs, R.attr.customImageButtonStyle );
        }
    
        public CustomImageButton( Context context, AttributeSet attrs,
                int defStyle ) {
            super( context, attrs, defStyle );
    
            final TypedArray array = context.obtainStyledAttributes( attrs,
                R.styleable.CustomImageButton, defStyle,
                R.style.Widget_ImageButton_Custom ); // see below
            this.customAttr =
                array.getString( R.styleable.CustomImageButton_customAttr, "" );
            array.recycle();
        }
    }
    

    现在您必须将Theme.Custom 应用于所有使用CustomImageButton(在AndroidManifest.xml 中)的活动:

    <activity android:name=".MyActivity" android:theme="@style/Theme.Custom"/>
    

    就是这样。现在CustomImageButton 尝试从当前主题的customImageButtonStyle 属性加载默认属性值。如果在主题中没有找到这样的属性或者属性的值为@null,那么将使用obtainStyledAttributes 的最后一个参数:在这种情况下为Widget.ImageButton.Custom

    您可以更改所有实例和所有文件的名称(AndroidManifest.xml 除外),但最好使用 Android 命名约定。

    【讨论】:

    • 有没有办法在不使用主题的情况下做同样的事情?我有一些自定义小部件,但我希望用户能够将这些小部件与他们想要的任何主题一起使用。按照您发布的方式进行操作需要用户使用我的主题。
    • @theDazzler:如果您的意思是开发人员可以使用的库,那么他们将能够创建自己的主题并使用主题中的样式属性指定小部件的样式(customImageButtonStyle in这个答案)。这就是在 Android 上自定义操作栏的方式。如果您的意思是在运行时更改主题,那么这种方法是不可能的。
    • @Michael,是的,我的意思是开发人员可以使用的库。你的评论解决了我的问题。谢谢!
    • 这一切最令人惊奇的是,谷歌的某个人认为这没问题。
    • name="CustomTheme"declare-styleable 属性包装器中是否有任何用途?那只是为了组织,因为我没有看到它在任何地方使用。我可以将各种小部件的所有样式属性放在一个包装器中吗?
    【解决方案2】:

    除了 michael 的出色回答之外,另一个方面是覆盖主题中的自定义属性。 假设您有许多自定义视图都引用自定义属性“custom_background”。

    <declare-styleable name="MyCustomStylables">
        <attr name="custom_background" format="color"/>
    </declare-styleable>
    

    在主题中,您定义值是什么

    <style name="MyColorfulTheme" parent="AppTheme">
        <item name="custom_background">#ff0000</item>
    </style>
    

    <style name="MyBoringTheme" parent="AppTheme">
        <item name="custom_background">#ffffff</item>
    </style>
    

    你可以引用样式中的属性

    <style name="MyDefaultLabelStyle" parent="AppTheme">
        <item name="android:background">?background_label</item>
    </style>
    

    注意问号,也用于参考android属性,如

    ?android:attr/colorBackground
    

    正如你们大多数人所注意到的,您可以 - 并且可能应该 - 使用 @color 引用而不是硬编码颜色。

    那为什么不干呢

    <item name="android:background">@color/my_background_color</item>
    

    您不能在运行时更改“my_background_color”的定义,但您可以轻松切换主题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多