html 添加窗口小部件,如何:为自定义窗口小部件定义主题(样式)项

d4a349cc0702438ab3dfdb4a67510125.png

波斯汪

是的,有一种方法:假设您有一个小部件的属性声明(在中attrs.xml):    声明用于样式参考的属性(在中attrs.xml):    为小部件声明一组默认属性值(在中styles.xml):声明自定义主题(在中themes.xml):将此属性用作窗口小部件的构造函数中的第三个参数(在中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中):就这样。现在CustomImageButton尝试从customImageButtonStyle当前主题的属性加载默认属性值。如果在主题或属性的值中未找到此类属性@null,obtainStyledAttributes则将使用的最后一个参数:Widget.ImageButton.Custom在这种情况下。您可以更改所有实例和所有文件的名称(除外AndroidManifest.xml),但是使用Android命名约定会更好。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值