自定义View(三) 自定义itemCheckView
2017-10-12 09:53
239 查看
自定义itemCheckView 实现效果如下:
自定义itemCheckView
选中.jpg
自定义View类实现
自定义View标签
自定义View 布局
自定义View 选择器
自定义View 素材
自定义View属性
Activity使用自定义View
欢迎关注微信公众号:程序员Android
公众号ID:ProgramAndroid
获取更多信息
微信公众号:ProgramAndroid
我们不是牛逼的程序员,我们只是程序开发中的垫脚石。
我们不发送红包,我们只是红包的搬运工。
1.注意 :自定义 Android 命名空间
xmlns:programandroid="http://schemas.android.com/apk/res/com.programandroid"
注意:自定义View 属性
programandroid:desOff=" 不选中"
programandroid:desOn=" 选中"
programandroid:desTitle=" WIFI "
属性声明在res/values/attrs.xml中定义
CheckBox 自定义样式
自定义 item选择器
btn_checkbox_normal.png
btn_checkbox_press.png
res/values/attrs.xml中定义
至此,自定义itemCheckView已经完成。
欢迎关注微信公众号:程序员Android
公众号ID:ProgramAndroid
获取更多信息
微信公众号:ProgramAndroid
我们不是牛逼的程序员,我们只是程序开发中的垫脚石。
我们不发送红包,我们只是红包的搬运工。
点击阅读原文,获取更多福利
自定义itemCheckView
选中.jpg
自定义View类实现
自定义View标签
自定义View 布局
自定义View 选择器
自定义View 素材
自定义View属性
Activity使用自定义View
欢迎关注微信公众号:程序员Android
公众号ID:ProgramAndroid
获取更多信息
微信公众号:ProgramAndroid
我们不是牛逼的程序员,我们只是程序开发中的垫脚石。
我们不发送红包,我们只是红包的搬运工。
1. 自定义View类实现
public class ItemCheckView extends RelativeLayout { // 每一个xml中的结点,都需要转换成java中的对象才可以去运行 // 转换成一个对象,调用其构造方法,在调用构造方法的时候,去做xml--->view操作 private static final String TAG = "ItemCheckView"; private TextView tv_title; private TextView tv_des; private CheckBox cb_box; private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.programandroid"; private String mDesTitle; private String mDesOff; private String mDesOn; public ItemCheckView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initAttrs(attrs); initUI(context); } public ItemCheckView(Context context, AttributeSet attrs) { super(context, attrs); initAttrs(attrs); initUI(context); } public ItemCheckView(Context context) { super(context); initUI(context); } // 单独抽取出来的 xml--->view private void initUI(Context context) { View.inflate(context, R.layout.item_check_view, this); tv_title = (TextView) findViewById(R.id.tv_title); tv_des = (TextView) findViewById(R.id.tv_des); cb_box = (CheckBox) findViewById(R.id.cb_box); tv_title.setText(mDesTitle); } // checkbox是否选中的状态,就决定了当前条目是否开启版本更新检测 // checbbox选中状态,和版本更新状态绑定 /** * 当前条目的选中状态,是否开启更新的选中状态 */ public boolean isCheck() { // 是否要去更新,由checkbox选中状态决定 true选中 false未选中 return cb_box.isChecked(); } /** * @param isCheck * 传递一个选中未选中的状态变量(true 选中 false未选中) */ public void setCheck(boolean isCheck) { cb_box.setChecked(isCheck); if (isCheck) { tv_des.setText(mDesOn); } else { tv_des.setText(mDesOff); } } /** * @param attrs * 包含了属性名称和属性值的set集合 */ private void initAttrs(AttributeSet attrs) { // 打印属性总个数 /* * Log.i(tag, "attrs.getAttributeCount() = "+attrs.getAttributeCount()); * for(int i=0;i<attrs.getAttributeCount();i++){ //获取所有的属性名称 Log.i(tag, * "属性名称 = "+attrs.getAttributeName(i)); Log.i(tag, * "属性值 = "+attrs.getAttributeValue(i)); } */ mDesTitle = attrs.getAttributeValue(NAMESPACE, "desTitle"); mDesOff = attrs.getAttributeValue(NAMESPACE, "desOff"); mDesOn = attrs.getAttributeValue(NAMESPACE, "desOn"); Log.i(TAG, mDesTitle); Log.i(TAG, mDesOff); Log.i(TAG, mDesOn); } }
2. 自定义View标签
1.注意 :自定义 Android 命名空间xmlns:programandroid="http://schemas.android.com/apk/res/com.programandroid"
注意:自定义View 属性
programandroid:desOff=" 不选中"
programandroid:desOn=" 选中"
programandroid:desTitle=" WIFI "
属性声明在res/values/attrs.xml中定义
<com.programandroid.CustomView.ItemCheckView xmlns:programandroid="http://schemas.android.com/apk/res/com.programandroid" android:id="@+id/custom_item_check_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/listview_item_selector" programandroid:desOff=" 不选中" programandroid:desOn=" 选中" programandroid:desTitle=" WIFI " />
3. 自定义View 布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" > <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="sss" android:textColor="@android:color/primary_text_light" android:textSize="18sp" /> <TextView android:id="@+id/tv_des" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_title" android:text="sss" android:textColor="@android:color/secondary_text_light" android:textSize="14sp" /> <CheckBox android:id="@+id/cb_box" style="@style/CustomCheckboxTheme" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" /> </RelativeLayout>
4. 自定义View 选择器
CheckBox 自定义样式<!-- 自定义CheckBox 样式 --> <style name="CustomCheckboxTheme" parent="@android:style/Widget.CompoundButton.CheckBox"> <item name="android:button">@drawable/custom_checkbox_selector</item> </style>
自定义 item选择器
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 按下去的背景颜色显示效果 --> <item android:drawable="@drawable/list_item_bg_light_pressed" android:state_pressed="true"/> <!-- 获取焦点时背景颜色显示效果 --> <item android:drawable="@drawable/list_item_bg_light_pressed" android:state_focused="true"/> <!-- 没有任何状态下的背景颜色 --> <item android:drawable="@drawable/list_item_bg_light_normal"/> </selector>
5. 自定义View 素材
btn_checkbox_normal.png
btn_checkbox_press.png
6. 自定义view 属性
res/values/attrs.xml中定义<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="ItemCheckView"><!--SettingItemView申明控件,内部可以包含自定义属性--> <attr name="desTitle" format="string"/><!--标题描述属性--> <attr name="desOff" format="string"/><!--关闭更新属性--> <attr name="desOn" format="string"/><!--开启更新属性--> </declare-styleable> </resources>
7. Activity使用自定义View
/** * 自定义ItemCheckView */ private void InitItemCheckView() { // TODO Auto-generated method stub final ItemCheckView mItemCheckView = (ItemCheckView) findViewById(R.id.custom_item_check_view); mItemCheckView.setCheck(false); mItemCheckView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 1,获取当前条的原有状态 boolean check = mItemCheckView.isCheck(); // 2,对第一步获取的状态取反 mItemCheckView.setCheck(!check); if (!check) { } else { } } }); }
至此,自定义itemCheckView已经完成。
欢迎关注微信公众号:程序员Android
公众号ID:ProgramAndroid
获取更多信息
微信公众号:ProgramAndroid
我们不是牛逼的程序员,我们只是程序开发中的垫脚石。
我们不发送红包,我们只是红包的搬运工。
点击阅读原文,获取更多福利
相关文章推荐
- 自定义RecyclerView.ItemDecoration,实现RecyclerView的分割线效果
- listview添加headerview需要注意的,listview自定义的item注意的焦点
- 如何自定义RecycleView item的间距
- Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中
- Android学习自定义View(四)——继承控件(滑动时ListView的Item出现删除按钮)
- Android RecyclerView ItemTouchHelper、自定义LayoutManager
- RecyclerView自定义动画,item飞入,渐隐渐出
- android 自定义TextView支持微博功能后在ListView占用了Item点击的解决办法
- Android学习自定义View(四)——继承控件(滑动时ListView的Item出现删除按钮)
- recycleview的 item的自动居中以及自定义左对齐集代码分析
- 自定义RecyclerView的item的点击事件
- Android 自定义ViewGroup实现整个Item布局竖直跑马灯效果
- 获取自定义item的ListView里的View中的内容,如Text
- RecyclerView基础-数据适配、自定义分割线、上拉加载、下拉加载、item点击事件
- 自定义导航栏的时候使用UIImagePickerViewControlle Item无法正常显示
- 解决listview的item为自定义viewgroup时,viewgroup子view press时,整个viewgroup都被按下的问题
- android 自定义TextView支持微博功能后在ListView占用了Item点击的解决办法
- Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析
- 自定义switch,CircularProgressView,check,segment
- 自定义宽高比例的view(轮播图,item等)