700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > html 添加窗口小部件 如何:为自定义窗口小部件定义主题(样式)项

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

时间:2022-06-01 06:06:54

相关推荐

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

波斯汪

是的,有一种方法:假设您有一个小部件的属性声明(在中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命名约定会更好。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。