一、定义一个XML布局文件
setting_item_vIEw.xml
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="60dip" > <TextVIEw androID:ID="@+ID/tv_Title" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentleft="true" androID:layout_marginleft="5dip" androID:layout_margintop="5dip" androID:textcolor="#000000" androID:textSize="20dip" /> <TextVIEw androID:ID="@+ID/tv_desc" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_below="@+ID/tv_Title" androID:layout_marginleft="5dip" androID:layout_marginBottom="5dip" androID:textcolor="#99000000" androID:textSize="18dip" /> <CheckBox androID:clickable="false" androID:focusable="false" androID:ID="@+ID/cb_status" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentRight="true" androID:layout_centerVertical="true" androID:layout_marginRight="20dip" /> <VIEw androID:layout_wIDth="fill_parent" androID:layout_height="0.2dip" androID:layout_alignParentBottom="true" androID:layout_alignParentleft="true" androID:layout_marginleft="5dip" androID:layout_marginRight="5dip" androID:background="#000000" /></relativeLayout>
二、在src/values/attrs.xml中定义属性
<?xml version="1.0" enCoding="utf-8"?><resources> <declare-styleable name="TextVIEw"> <attr name="Title" format="string" /> <attr name="desc_on" format="string" /> <attr name="desc_off" format="string" /> </declare-styleable></resources>
三、自定义一个vIEw继承自你需要的布局
inivIEw(Context context)初始化自定义的布局文件
根据需求自定义一些API方法
public class SettingItemVIEw extends relativeLayout { private CheckBox cb_status; private TextVIEw tv_Title; private TextVIEw tv_desc; private String Title; private String desc_on; private String desc_off; public voID inivIEw(Context context){ VIEw.inflate(context,R.layout.setting_item_vIEw,this); cb_status = (CheckBox)findVIEwByID(R.ID.cb_status); tv_Title = (TextVIEw)findVIEwByID(R.ID.tv_Title); tv_desc = (TextVIEw)findVIEwByID(R.ID.tv_desc); } public SettingItemVIEw(Context context,AttributeSet attrs,int defStyle) { super(context,attrs,defStyle); inivIEw(context); } public SettingItemVIEw(Context context,AttributeSet attrs) { super(context,attrs); inivIEw(context); Title = attrs.getAttributeValue("http://schemas.androID.com/apk/res/com.victor.mobilesafe","Title"); desc_on = attrs.getAttributeValue("http://schemas.androID.com/apk/res/com.victor.mobilesafe","desc_on"); desc_off = attrs.getAttributeValue("http://schemas.androID.com/apk/res/com.victor.mobilesafe","desc_off"); tv_Title.setText(Title); setDesc(desc_off); } public SettingItemVIEw(Context context) { super(context); inivIEw(context); } public boolean isChecked(){ return cb_status.isChecked(); } public voID setChecked(boolean checked){ if (checked) { setDesc(desc_on); }else{ setDesc(desc_off); } cb_status.setChecked(checked); } public voID setDesc(String text){ tv_desc.setText(text); }}
四、在布局文件中使用该自定义组合控件
别忘记声明自定义命名空间
xmlns:victor="http://schemas.androID.com/apk/res/com.victor.mobilesafe"
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:victor="http://schemas.androID.com/apk/res/com.victor.mobilesafe" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" > <com.victor.mobilesafe.ui.SettingItemVIEw androID:ID="@+ID/siv_update" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" victor:desc_off="自动更新关闭" victor:desc_on="自动更新开启" victor:title="设置自动更新" > </com.victor.mobilesafe.ui.SettingItemVIEw></linearLayout>
总结:
1.自定义一个VIEw 一般来说,继承相对布局,或者线性布局 VIEwGroup;
2.实现父类的构造方法。一般来说,需要在构造方法里初始化自定义的布局文件;
3.根据一些需要或者需求,定义一些API方法;
4.根据需要,自定义控件的属性,可以参照TextVIEw属性;
5.自定义命名空间,例如:
xmlns:victor="http://schemas.androID.com/apk/res/<包名>"xmlns:victor="http://schemas.androID.com/apk/res/com.victor.mobilesafe"
6.自定义我们的属性,在Res/values/attrs.xml
7.使用我们自定义的属性
例如:
itheima:title="设置自动更新" itheima:desc_on="设置自动更新已经开启" itheima:desc_off="设置自动更新已经关闭"
8.在我们自定义控件的带有两个参数的构造方法里AttributeSet attrs 取出我们的属性值,关联自定义布局文件对应的控件。
总结以上是内存溢出为你收集整理的在Android开发中使用自定义组合控件的例子全部内容,希望文章能够帮你解决在Android开发中使用自定义组合控件的例子所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)