Android自定义控件封装之自定义属性的实现
来源:互联网 发布:网络炒作 编辑:程序博客网 时间:2024/05/15 16:33
在开发中有时候我们需要去自定义一些组合控件,而在使用过程中,又想要自己的组合控件能有原生控件那样可以在xml中使用属性控制,那么我们就需要去自定义一些属性了
1:首先在values/attrs.xml中进行属性的定义
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="MyAppTitle"> <attr name="TitleText" format="string"/> <attr name="LeftText" format="string"/> <attr name="RightText" format="string"/> </declare-styleable> </resources>
2:在定义好这些属性后,就需要在自己自定义的类中进行获取和操作了
public MyAppTitle(Context context, AttributeSet attrs) { super(context, attrs); TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.MyAppTitle); mLeftText = ta.getString(R.styleable.MyAppTitle_LeftText); mTitleText = ta.getString(R.styleable.MyAppTitle_TitleText); mRightText = ta.getString(R.styleable.MyAppTitle_RightText); Log.e("Fizzer",mLeftText+mRightText+mTitleText);}
3:接下来就可以在xml文件布局中进行使用了
<com.fizzer.anbangproject_dahuo_test.Widget.MyAppTitle android:id="@+id/viewMyAppTitle" android:layout_width="match_parent" android:layout_height="wrap_content" fizzer:TitleText="标题" fizzer:LeftText="左边" fizzer:RightText="右边"/>
xmlns:fizzer="http://schemas.android.com/apk/res-auto"
4:最后说一点的就是,在attrs文件中styleable中的命名是可以自定义的,但是为了在书写布局文件时有属性的提示功能,所以最好还是保持自定义控件的类名与styleable的命名一致,当然,Google也支持和鼓励开发者使用对应的类名来作为styleable的命名
2 1
- Android自定义控件封装之自定义属性的实现
- Android中给自定义的控件实现自定义的属性
- Android自定义控件的自定义属性2种实现方式
- Android自定义组合控件之自定义属性
- Android自定义控件之自定义属性解析
- Android自定义控件之自定义属性
- Android自定义控件之自定义属性
- Android自定义控件之自定义属性
- Android自定义控件的自定义属性
- android自定义控件的自定义属性
- Android自定义控件的属性
- Android自定义控件的属性
- Android 自定义控件的属性
- Android自定义控件之app标题栏的封装
- 自定义控件之自定义属性
- Android自定义控件 自定义属性
- Android 自定义控件 自定义属性
- android 自定义控件自定义属性
- 简笔画检索“Sketch Me That Shoe”
- 5.Linux下----C语言编辑语法高亮(原创)
- 使用Log Parser将IIS日志导入SQL分析
- bzoj1082: [SCOI2005]栅栏
- tjut 3436
- Android自定义控件封装之自定义属性的实现
- [.Net码农]asp.net三级嵌套repeater
- 文章标题
- Java NIO原理分析
- logstash: 拒绝连接 {:class=>"Manticore::SocketException", :level=>:error}
- git常用命令
- 梦之鬼索MVP模式在Android中的设计和实现
- Cadence 17.2常用快捷键
- Android Camera中无法回调PictureCallback接口onPictureTaken()函数