Android控件_自定义组合控件

来源:互联网 发布:存在主义 知乎 编辑:程序博客网 时间:2024/04/25 11:48


最近很多地方用到这个...详细写下

就先不在公司项目里写了,暂时在demo里大概写下具体实现步骤....


1.写一个SettingView类,继承 RelativeLayout ,并实现他的三个构造方法

package com.example.demo;import android.content.Context;import android.util.AttributeSet;import android.widget.RelativeLayout;public class SettingView extends RelativeLayout {public SettingView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stub}public SettingView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}public SettingView(Context context) {super(context);// TODO Auto-generated constructor stub}}

如果在代码中直接new的方式使用自定义控件,会执行一个参数的构造方法,如果在布局文件中使用自定义控件的时候,会执行两个参数的构造方法,如果在布局文件中使用自定义控件的时候使用了style样式,就会执行三个参数的构造方法


2.写好布局文件,是多个控件组成的组合控件,效果如下,并在SettingView中构造方法中初始化布局


布局代码就不详细贴出来了....OK...继续

初始化该布局.....

//初始化布局private void initUI(Context context){View view = View.inflate(context, R.layout.setting_view, null);//把view挂载到当前自定义布局上this.addView(view);}


上面代码也可以简写成   
View view = View.inflate(context, R.layout.setting_view, this);

这样,就可以在代码里直接使用自定义的控件了

SettingView sv = new SettingView(this);

setContentView(sv);

但是需求应该是要在布局文件里直接使用自定义的组合控件,在布局文件中使用自定义控件看下效果

<com.example.demo.SettingView        android:layout_width="fill_parent"        android:layout_height="55dip"        />    <com.example.demo.SettingView        android:layout_width="fill_parent"        android:layout_height="55dip"        />    <com.example.demo.SettingView        android:layout_width="fill_parent"        android:layout_height="55dip"        />


现在显示的文字都是在view中固定写死的,当然肯定是要暴露出修改标题和内容的方法....

3.在SettingView 中写修改图片,标题,内容...等的方法

//初始化布局private void initUI(Context context){View view = View.inflate(context, R.layout.setting_view, null);//把view挂载到当前自定义布局上this.addView(view);tv_title = (TextView) view.findViewById(R.id.tv_title);tv_content = (TextView) view.findViewById(R.id.tv_content);}//设置通知标题public void setTitle(String text){tv_title.setText(text);}//设置通知内容public void setContent(String text){tv_content.setText(text);}

这样写只能在代码中通过调用方法改变控件显示内容,不能直接在XML布局文件中直接设置控件上显示的内容.....

4.实现在XML布局文件中直接设置自定义控件上显示的内容

 在XML布局中     kacha:title=""     kacha:content=""  .....


自己定义一个命名空间

系统命名空间:xmlns:android="http://schemas.android.com/apk/res/android"    前面的写法是固定的,后面的android实质上是指向系统jar包

如果想自己定义命名空间,那么后面应该指向的是本程序的包名。所以自定义的命名空间为:

xmlns:kacha="http://schemas.android.com/apk/res/com.example.demo"


做好以上工作,然后接着自定义需要使用的属性...title content....   类似android命名空间下的 text... src...等属性

在value下创建attrs.xml文件,自定义属性集

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="SrttingView">        <attr name="title" format="string"/>        <attr name="content" format="string"/>    </declare-styleable></resources>
在R文件中会自动生成两个属性字段




ok...增加属性完成....接着完成对属性的使用.....


5.完成对自定义属性的使用

在构造方法中的attrs中存放的是XML布局文件里自定义的属性集合

public SettingView(Context context, AttributeSet attrs) {super(context, attrs);initUI(context);//从全部的属性集合中过滤出来自定义的属性数组//SrttingView是在R文件中自动生成的自定义属性数组//返回所有自定义属性和自定义属性的值的数组TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.SrttingView);String title = arr.getString(R.styleable.SrttingView_title);String content = arr.getString(R.styleable.SrttingView_content);tv_title.setText(title);tv_content.setText(content);}


ok..这样就能在XML布局文件中直接用  kacha:title=""      kacha:content=""  的方式设置对应的值了...

6.释放资源

 arr.recycle();


7.总结下自定义控件的步骤

>写一个SettingView类,继承RelativeLayout 

>定义一个XML布局文件,把布局转化成View对象,加到SettingView,添加的操作写在构造方法中

>为了能够实现一些自定义的属性,自定义命名空间
xmlns:kacha="http://schemas.android.com/apk/res/com.example.demo"

>自定义一些属性,在values目录下创建attrs.xml文件,声明自定义的属性

<declare-styleable name="SrttingView">        <attr name="title" format="string"/>        <attr name="content" format="string"/>    </declare-styleable>

>自动在R文件中创建内部类 styleable 属性的集合 attrs 属性

>在xml文件中使用 <com.example.demo.SettingView /> 自定义的view对象

 使用 kacha:title=“” 设置通知标题

>如果在xml文件中使用自定义的view对象,会执行有两个参数的构造方法   AttributeSet 所有的属性集

>把自定义的属性过滤出来 TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.SrttingView);  返回值是TypedArray

>获取里面的数据  arr.getstring()  设置到对应的view对象

>释放资源 arr.recycle();


ok。结束.....

0 0