如何:定义自定义小部件的主题(样式)项

发布于 2024-10-09 02:54:07 字数 247 浏览 8 评论 0原文

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

I've written a custom widget for a control that we use widely throughout our application. The widget class derives from ImageButton and extends it in a couple of simple ways. I've defined a style which I can apply to the widget as it's used, but I'd prefer to set this up through a theme. In R.styleable I see widget style attributes like imageButtonStyle and textViewStyle. Is there any way to create something like that for the custom widget I wrote?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隱形的亼 2024-10-16 02:54:07

是的,有一种方法:

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

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

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

<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 命名约定。

Yes, there's one way:

Suppose you have a declaration of attributes for your widget (in attrs.xml):

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

Declare an attribute you will use for a style reference (in attrs.xml):

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

Declare a set of default attribute values for the widget (in styles.xml):

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

Declare a custom theme (in themes.xml):

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

Use this attribute as the third argument in your widget's constructor (in 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();
    }
}

Now you have to apply Theme.Custom to all activities that use CustomImageButton (in AndroidManifest.xml):

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

That's all. Now CustomImageButton tries to load default attribute values from customImageButtonStyle attribute of current theme. If no such attribute is found in the theme or attribute's value is @null then the final argument to obtainStyledAttributes will be used: Widget.ImageButton.Custom in this case.

You can change names of all instances and all files (except AndroidManifest.xml) but it would be better to use Android naming convention.

孤独岁月 2024-10-16 02:54:07

除了迈克尔的出色答案之外,另一个方面是覆盖主题中的自定义属性。
假设您有许多自定义视图,它们都引用自定义属性“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”的定义,而你可以轻松切换主题。

Another aspect in addition to michael's excellent answer is overriding custom attributes in themes.
Suppose you have a number of custom views that all refer to the custom attribute "custom_background".

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

In a theme you define what the value is

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

or

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

You can refer to the attribute in a style

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

Notice the question mark, as also used for reference android attribute as in

?android:attr/colorBackground

As most of you have noticed, you can -and probably should- use @color references instead of hard coded colors.

So why not just do

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

You can not change the definition of "my_background_color" at runtime, whereas you can easily switch themes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文