html 添加窗口小部件,如何:为自定义窗口小部件定义主题(样式)项
波斯汪
是的,有一種方法:假設(shè)您有一個(gè)小部件的屬性聲明(在中attrs.xml):? ? 聲明用于樣式參考的屬性(在中attrs.xml):? ? 為小部件聲明一組默認(rèn)屬性值(在中styles.xml):聲明自定義主題(在中themes.xml):將此屬性用作窗口小部件的構(gòu)造函數(shù)中的第三個(gè)參數(shù)(在中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();? ? }}現(xiàn)在,您必須應(yīng)用Theme.Custom到所有使用的活動(dòng)CustomImageButton(在AndroidManifest.xml中):就這樣。現(xiàn)在CustomImageButton嘗試從customImageButtonStyle當(dāng)前主題的屬性加載默認(rèn)屬性值。如果在主題或?qū)傩缘闹抵形凑业酱祟悓傩?#64;null,obtainStyledAttributes則將使用的最后一個(gè)參數(shù):Widget.ImageButton.Custom在這種情況下。您可以更改所有實(shí)例和所有文件的名稱(除外AndroidManifest.xml),但是使用Android命名約定會(huì)更好。
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的html 添加窗口小部件,如何:为自定义窗口小部件定义主题(样式)项的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java与MySQL做购物系统_java
- 下一篇: java 包含汉字,java判断字符串中