带有互动的网站开发网页设计素材 模板材料
news/
2025/9/22 20:05:27/
文章来源:
带有互动的网站开发,网页设计素材 模板材料,阿里云网站备案注销吗,水泥网站营销方案怎么做安卓自定义属性主要有3个步骤
在values文件夹新建attrs.xml文件中声明属性#xff0c;包括属性名和格式#xff0c;format常用属性有string ,integer,reference等
?xml version1.0 encodingutf-8?
resources!-- 声明属性集…安卓自定义属性主要有3个步骤
在values文件夹新建attrs.xml文件中声明属性包括属性名和格式format常用属性有string ,integer,reference等
?xml version1.0 encodingutf-8?
resources!-- 声明属性集的名称 --declare-styleable nameMyToggleButtton!-- 声明属性的name与类型 --attr namemy_background formatreference/attr namemy_slide_btn formatreference/attr namecurr_state formatboolean//declare-styleable/resources
在布局文件中使用使用之前必须先声明命名空间前面是固定不变的内容后面是包名.
RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:zjhttp://schemas.android.com/apk/res/com.zj.switchbuttonxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context${relativePackage}.${activityClass} com.zj.switchbutton.MyTrouggleButtonandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentzj:my_backgrounddrawable/switch_backgroundzj:my_slide_btndrawable/slide_buttonzj:curr_statetrue//RelativeLayout
在自定义view的构造方法中通过解析AttributeSet方法获得所需要的属性值,解析AttributeSet主要有两种方法
第一种:通过attrs.getAttributeValue获得
int countsattrs.getAttributeCount();for(int i0;icounts;i){attrs.getAttributeName(i);attrs.getAttributeValue(i);}public SettingItemView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubiniView(context);String title attrs.getAttributeValue(http://schemas.android.com/apk/res/com.zj.mobilesafe, mytitle);desc_on attrs.getAttributeValue(http://schemas.android.com/apk/res/com.zj.mobilesafe, desc_on);desc_off attrs.getAttributeValue(http://schemas.android.com/apk/res/com.zj.mobilesafe, desc_off);tv_title.setText(title);setDesc(desc_off);}
第二种:通过TypedArray获得
public MyTrouggleButton(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub//获得自定义属性TypedArray tacontext.obtainStyledAttributes(attrs,R.styleable.MyToggleButtton);int Nta.getIndexCount();for(int i0;iN;i){int itemIdta.getIndex(i);switch (itemId) {case R.styleable.MyToggleButtton_curr_state:current_stateta.getBoolean(itemId, false);break;case R.styleable.MyToggleButtton_my_background:backgroundIDta.getResourceId(itemId, -1);if(backgroundID-1){throw new RuntimeException(请设置背景图片);}backgroundBitmapBitmapFactory.decodeResource(getResources(),backgroundID);break;case R.styleable.MyToggleButtton_my_slide_btn:slideButtonIDta.getResourceId(itemId, -1);if(backgroundID-1){throw new RuntimeException(请设置图片);}slideBtnBitmapBitmapFactory.decodeResource(getResources(), slideButtonID);default:break;}}init();}自定义属性到底有什么用呢当界面上的自定义元素有一些值需要改变并且大量重复的时候自定义属性可以有效的提高代码的重用性下面是一个简单的例子
声明属性
?xml version1.0 encodingutf-8?
resourcesdeclare-styleable nameTextViewattr namemytitle formatstring /attr namedesc_on formatstring /attr namedesc_off formatstring //declare-styleable
/resources
在xml文件中定义
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:zjhttp://schemas.android.com/apk/res/com.zj.mobilesafeandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationvertical TextViewandroid:idid/textView1android:layout_widthfill_parentandroid:layout_height55dipandroid:background#8866ff00android:gravitycenterandroid:text设置中心android:textColor#000000android:textSize22sp /com.zj.mobilesafe.ui.SettingItemViewandroid:idid/siv_updateandroid:layout_widthwrap_contentandroid:layout_height65dipzj:desc_off设置自动更新已经关闭zj:desc_on设置自动更新已经开启zj:mytitle设置自动更新 /com.zj.mobilesafe.ui.SettingItemViewcom.zj.mobilesafe.ui.SettingItemViewandroid:idid/siv_show_addressandroid:layout_widthwrap_contentandroid:layout_height65dipzj:desc_off设置显示号码归属地已经关闭zj:desc_on设置显示号码归属地已经开启zj:mytitle设置显示号码归属地 /com.zj.mobilesafe.ui.SettingItemViewcom.zj.mobilesafe.ui.SettingClickViewandroid:idid/scv_changebgandroid:layout_widthwrap_contentandroid:layout_height65dip/com.zj.mobilesafe.ui.SettingClickViewcom.zj.mobilesafe.ui.SettingItemViewandroid:idid/siv_callsms_safeandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentzj:desc_off黑名单拦截已经关闭zj:desc_on黑名单拦截已经开启zj:mytitle黑名单拦截设置 /com.zj.mobilesafe.ui.SettingItemViewcom.zj.mobilesafe.ui.SettingItemViewandroid:idid/siv_watchdogandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentzj:desc_off看门狗已经关闭zj:desc_on看门狗已经开启zj:mytitle程序锁设置 /com.zj.mobilesafe.ui.SettingItemView/LinearLayout
解析属性并且改变属性
/*** 自定义的组合控件* author Administrator**/
public class SettingItemView extends RelativeLayout {private CheckBox cb_status;private TextView tv_desc;private TextView tv_title;private String desc_on;private String desc_off;/*** 初始化布局文件* param context*/private void iniView(Context context) {// TODO Auto-generated method stubView.inflate(context, R.layout.setting_item_view, SettingItemView.this);cb_status(CheckBox) this.findViewById(R.id.cb_status);tv_desc(TextView) this.findViewById(R.id.tv_desc);tv_title(TextView) this.findViewById(R.id.tv_title);}public SettingItemView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stubiniView(context);}/*** 带有两个参数的构造方法,布局文件使用的时候调用 * param context* param attrs*/public SettingItemView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubiniView(context);String title attrs.getAttributeValue(http://schemas.android.com/apk/res/com.zj.mobilesafe, mytitle);desc_on attrs.getAttributeValue(http://schemas.android.com/apk/res/com.zj.mobilesafe, desc_on);desc_off attrs.getAttributeValue(http://schemas.android.com/apk/res/com.zj.mobilesafe, desc_off);tv_title.setText(title);setDesc(desc_off);}public SettingItemView(Context context) {super(context);// TODO Auto-generated constructor stubiniView(context);}/*** * 检验组合和控件是否有焦点*/public boolean isChecked(){return cb_status.isChecked();}/*** 设置组合控件的是否选中*/public void setChecked(boolean checked){if(checked){setDesc(desc_on);}else{setDesc(desc_off);}cb_status.setChecked(checked);}/*** 组合控件 的内容发生改变* */public void setDesc(String text){tv_desc.setText(text);}}
效果如下
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/910216.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!