Android 自定义组合控件,属性
来源:互联网 发布:js classname 编辑:程序博客网 时间:2024/05/18 03:02
自定义组合控件
1.创建自定义控件继承RelativeLayout
public class SettingView extends RelativeLayout { 2.创建一个init方法,并在每个构造函数中调用 /** * 添加控件 */ private void init(){ //添加布局文件 // TextView textView = new TextView(getContext()); // textView.setText("我是自定义组合控件的textview"); //第一种方式 //将布局文件转化成view对象 // View view = View.inflate(getContext(), R.layout.settingview, null);//爹有了,去找孩子,亲生 // //添加操作 // this.addView(view);//在自定义组合控件中添加一个textview //第二种方式 //获取view对象,同时给veiw对象设置父控件,相当于先创建一个view对象,在把控件放到自定义控件中 View.inflate(getContext(), R.layout.settingview, this);//孩子有了,去找爹,喜当爹 }
自定义属性
1.在values->attrs.xml<resources><declare-styleable name="包名.ui.SettingView"> <attr name="title" format="string" /><!-- name:属性的名称,format:类型 --> <attr name="des_on" format="string" /> <attr name="des_off" format="string" /></declare-styleable>
布局中使用
a.命名空间
xmlns:itheima=”http://schemas.android.com/apk/res/包名”
b.控件中使用
<包名.ui.SettingView
android:id=”@+id/sv_setting_update”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
itheima:title=”提示更新”
itheima:des_on=”打开提示更新”
itheima:des_off=”关闭提示更新”
String title = attrs.getAttributeValue("http://schemas.android.com/apk/res/包名", "title"); des_on = attrs.getAttributeValue("http://schemas.android.com/apk/res/包名", "des_on"); des_off = attrs.getAttributeValue("http://schemas.android.com/apk/res/包名", "des_off");
//给自定义组合控件的控件设置相应的值 //初始化控件的值
tv_setting_title.setText(title); if (isChecked()) { tv_setting_des.setText(des_on); }else{ tv_setting_des.setText(des_off); }
0 0
- Android 自定义组合控件,属性
- Android自定义组合控件之自定义属性
- Android组合控件详解 & 自定义属性
- 组合控件-自定义属性
- Android自定义控件---组合控件(包括自定义属性)
- 自定义控件:自定义组合控件+自定义属性
- Android自定义xml属性,自定义(组合)控件
- 自定义组合控件和属性
- Android自定义组合控件
- android自定义组合控件
- Android自定义组合控件
- android组合自定义控件
- android 自定义组合控件
- android 自定义组合控件
- android 自定义组合控件
- Android自定义组合控件
- android自定义组合控件
- Android 自定义组合控件
- SAP特性,物料特性,批次特性建立的BAPI函數
- 建筑业的未来效率依赖于物联网
- 自己写个 Prisma
- vue.js实现checkbox全选
- Android中使用Libgdx的FillViewPort在页面跳转之后失效
- Android 自定义组合控件,属性
- Search a 2D Matrix 解题报告
- Unity读取内部、外部资源详解
- mysql优化——explain详解
- bug初步访问时:mysql
- WordCount代码编写
- SQL charindex 通过id字符串获取数据
- SEO年度工作总结
- 图森未来算法科学家李定华浙江ACM省赛现场演讲