android自定义组件segmentedgroup源码阅读笔记
来源:互联网 发布:怎么设计数据库的表 编辑:程序博客网 时间:2024/05/29 17:57
Android自定义组件SegmentedGroup源码阅读笔记
类
- LayoutSelector(自定义): This class is used to provide the proper layout based on the view. Also provides the proper radius for corners. 根据view提供合适的布局, 同时也给四角提供了合适的半径
- Resources: Class for accessing an application’s resources. This sits on top of the asset manager of the application (accessible through getAssets()) and provides a high-level API for getting typed data from the assets. 访问应用资源的类。这个类在AssetManager之上,为从asset中得到数据提供了高等级的API
- ColorStateList: Lets you map View state sets to colors. ColorStateLists are created from XML resource files defined in the “color” subdirectory directory of an application’s resource directory. The XML file contains a single “selector” element with a number of “item” elements inside. 动态的color selector *
获取自定义数据并解析 initAttrs()方法
- TypedArray类似于键值对,可存储id,reference
- 使用Resources.getDimension(int dimenId)得到defValue
- 注意recycle
private void initAttrs(AttributeSet attrs) { TypedArray typedArray = getContext().getTheme().obtainStyledAttributes( attrs, R.styleable.SegmentedGroup, 0, 0); try { mMarginDp = (int) typedArray.getDimension( R.styleable.SegmentedGroup_border_width, getResources().getDimension(R.dimen.radio_button_stroke_border)); mCornerRadius = typedArray.getDimension( R.styleable.SegmentedGroup_corner_radius, getResources().getDimension(R.dimen.radio_button_conner_radius)); mTintColor = typedArray.getColor( R.styleable.SegmentedGroup_tint_color, getResources().getColor(R.color.radio_button_selected_color)); mCheckedTextColor = typedArray.getColor( R.styleable.SegmentedGroup_checked_text_color, getResources().getColor(android.R.color.white)); } finally { typedArray.recycle(); } }
动态StateList
- 动态设置按钮的selector, 二维数组[3][?] 对应一维数组[3]
ColorStateList colorStateList = new ColorStateList(new int[][]{ {android.R.attr.state_pressed}, {-android.R.attr.state_pressed, -android.R.attr.state_checked}, {-android.R.attr.state_pressed, android.R.attr.state_checked}}, new int[]{Color.GRAY, mTintColor, mCheckedTextColor}); ((Button) view).setTextColor(colorStateList);
- 动态设置子view的背景
Drawable checkedDrawable = resources.getDrawable(checked).mutate(); Drawable uncheckedDrawable = resources.getDrawable(unchecked).mutate(); ((GradientDrawable) checkedDrawable).setColor(mTintColor); ((GradientDrawable) checkedDrawable).setStroke(mMarginDp, mTintColor); ((GradientDrawable) uncheckedDrawable).setStroke(mMarginDp, mTintColor); //Set proper radius ((GradientDrawable) checkedDrawable).setCornerRadii(mLayoutSelector.getChildRadii(view)); ((GradientDrawable) uncheckedDrawable).setCornerRadii(mLayoutSelector.getChildRadii(view)); //Create drawable StateListDrawable stateListDrawable = new StateListDrawable(); stateListDrawable.addState(new int[]{-android.R.attr.state_checked}, uncheckedDrawable); stateListDrawable.addState(new int[]{android.R.attr.state_checked}, checkedDrawable); view.setBackground(stateListDrawable);
onFinishInflate()
Finalize inflating a view from XML. This is called as the last phase of inflation, after all child views have been added. 结束xml文件的inflate. 所有的view都已经被添加,最后阶段。
@Override protected void onFinishInflate() { super.onFinishInflate(); //Use holo light for default updateBackground(); }
自定义组件思路
1. 使用TpyedArray从attr获取数据,使用Resources获取默认数据2. 加载父控件,此时不需要自定义操作override3. 当所有子控件都已经填入,使用自定义的方法进行修饰,需要通过封装类考虑横竖屏的情况,此时需要使用到StateList, 自定义selector
问题总结
- context.getResources(), view.getResources()得到的Resources实例没有区别
- Resources中常用方法getColor(int colorId), getDimension(int dimenId), 用来获取colors.xml dimens.xml的id值
0 0
- android自定义组件segmentedgroup源码阅读笔记
- android自定义view组件circleprogress阅读笔记
- SegmentedGroup自定义控件的使用
- kube-scheduler 组件源码阅读笔记
- android笔记之自定义组件
- CI框架源码阅读笔记7 配置管理组件 Config.php
- CI框架源码阅读笔记7 配置管理组件 Config.php
- kube-scheduler 组件源码阅读笔记(二)
- Android应用层源码阅读笔记
- Android应用层源码阅读笔记
- Android源码阅读笔记(1)----Fragment
- Android学习笔记之自定义UI组件
- Android自定义组件开发详解笔记
- android源码阅读笔记1-配置源码路径/阅读源码方法讨论
- LZ77源码阅读笔记
- Boa 源码阅读笔记
- VNC源码阅读笔记
- osqa源码阅读笔记
- 简易HTTP服务器(多进程版本)
- Real time H.264 Encoding and Decoding-Using FFmpeg and x264
- 常用的网站压力测试工具有哪些
- 页面刷新一次设置变量加1
- IP数据报详解
- android自定义组件segmentedgroup源码阅读笔记
- Get windows environment variables
- (转)记一次内存优化的分享
- serviceManage
- 动态加载div动态加载tr动态加载ul
- 【已解决】NDK下Eclipse中交叉编译C项目出错:Symbol ‘NULL’ could not be resolved(JNI)
- R语言图层—利用ggplot画蝙蝠侠logo及分层
- Android自动开关机实现详细教程
- Android 应用程序分析之Settings.System.putInt