易采站长站为您分析在Android开发中使用自定义组合控件的例子,作者根据例子总结到了实现父类的构造方法等基本要点,具有一定参考价值,需要的朋友可以参考下
一、定义一个XML布局文件
setting_item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://www.easck.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>










