笔记97--自定义控件系列二
来源:互联网 发布:日记本软件 编辑:程序博客网 时间:2024/05/08 00:42
自定义控件的另一种实现:通过引用特定布局来生成自定义控件。
一、定义布局myview.xml
代码略。普通布局。
二、引用布局
在构造函数中将xml布局解析出来即可。
public class ImageBtnWithText extends LinearLayout {public ImageBtnWithText(Context context) {super(context);}public ImageBtnWithText(Context context, AttributeSet attrs) {super(context, attrs);LayoutInflater.from(context).inflate(R.layout.myview, this, true);}public ImageBtnWithText(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}}
三、使用这个控件
<com.example.mycontrol.ImageBtnWithText android:id="@+id/widget" android:layout_width="match_parent" android:layout_height="match_parent"/>当然之后也可以添加一些自定义属性。
四、attrs中自定义属性介绍
1、format:参数类型(可选项)。其取值为以下类型:
1)reference:引用
2)color:颜色
3)boolean:布尔值
4)dimension:尺寸值
5)float:浮点值
6)int:整型值
7)string:字符串
8)fraction:百分数,如200%
2、枚举值,格式如下:
<attr name="oriental"> <enum name="Horizontal" value="1"/> <enum name="Vertical" value="0"></enum></attr>xml中使用时:命名空间:oriental = "vertical"
3、标志位,位或运算
</pre><pre name="code" class="java"><pre name="code" class="java"><declare-styleable name="MRadioButton"> <attr name="windowSoftInputMode"> <flag name="stateUnspecified" value="0" /> <flag name="stateUnchanged" value="1" /> <flag name="stateHidden" value="2" /> <flag name="stateAlwaysHidden" value="3" /> <flag name="stateVisible" value="4" /> <flag name="stateAlwaysVisible" value="5" /> <flag name="adjustUnspecified" value="0x00" /> <flag name="adjustResize" value="0x10" /> <flag name="adjustPan" value="0x20" /> <flag name="adjustNothing" value="0x30" /> </attr></declare-styleable>
xml中使用时:
eoe:windowSoftInputMode="stateUnspecified | stateUnchanged"
4、另外,属性定义时可指定多种类型值
<attr name="background" format="reference|color"/>在xml中使用时:
eoe:background = "@drawable/图片id|#00FF00"
五、控件属性与xml定义相互绑定的函数TypedArray介绍
1、简介
TypedArray是一个存放资源的数组,首先从上下文中获取到R.styleable.View2这个属性资源的资源数组。记得,最后要调用recycle()来结束绑定。
说人话:先定义xml中属性,再定义.java中属性,然后把xml中属性取出来跟.java中属性绑定。关于绑定实现逻辑:从特定控件中获取属性数组存放到TypedArray中,然后遍历或者直接拿出某一个属性赋值给.java中的属性。
示例代码1:
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RadioButton); this.mValue = a.getString(R.styleable.RadioButton_value); a.recycle();示例代码2:
int resourceId = -1;TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.View2);TextView tv = new TextView(context);EditText et = new EditText(context);int n = typeArray.getIndexCount();for(int i=0; i<n; i++){int attr = typeArray.getIndex(i);switch (attr) {case R.styleable.View2_oriental:resourceId = typeArray.getInt(R.styleable.View2_oriental, 0);this.setOrientation(resourceId==1?LinearLayout.HORIZONTAL:LinearLayout.VERTICAL);break;case R.styleable.View2_text:resourceId = typeArray.getResourceId(R.styleable.View2_text, 0);tv.setText(resourceId>0?typeArray.getResources().getText(resourceId):typeArray.getString(R.styleable.View2_text));break;}}addView(tv);addView(et);typeArray.recycle();
2、方法介绍
TypedArray.getXX():基本上都是这样,需要传一个int型参数,传入在attrs中定义的名称即可。如R.styleable.View2_oriental(名字_属性)
TypedArray.obtainStyledAttributes()
0 0
- 笔记97--自定义控件系列二
- 自定义控件自学笔记(二)
- Android自定义控件系列案例【二】
- 笔记96--自定义控件系列一
- Android自定义控件系列二:自定义开关按钮(一)
- Android自定义控件系列三:自定义开关按钮(二)
- Android自定义控件系列二:自定义开关按钮
- Android自定义控件系列三:自定义开关按钮(二)
- Android自定义控件系列二:自定义开关按钮(一)
- Android自定义控件系列三:自定义开关按钮(二)
- Android自定义控件系列二:自定义开关按钮(一)
- Android自定义控件系列三:自定义开关按钮(二)
- Android自定义控件系列二:自定义开关按钮(一)
- Android自定义控件系列三:自定义开关按钮(二)
- Android自定义控件系列二:如何自定义属性
- 自定义控件学习笔记(二)
- 【我的应用系列】(二)自定义控件开发
- 【自定义控件系列二】android画图类---Paint浅谈
- 课堂小结
- delphi7 添加synedit
- 九度1023:EXCEL排序
- OGNL, JSTL, STRUTS2标签中符号#,$,%的用法示例
- 模仿微信侧滑按钮
- 笔记97--自定义控件系列二
- java基础---String学习笔记一
- Excel Sheet Column Title
- Andriod Notification的使用
- 从一个应用程序挑到另一个应用程序
- cin 流的状态
- Objective-C语法之代码块(block)的使用
- 学习开发工具汇总
- Linux安装java步骤