1.第一种方式:组合控件
1>定义:即将一些小的控件组合起来形成一个新的控件,这些小的控件多是系统自带的控件,
比如很多应用中普遍使用的标题栏控件
2>思路:
2.1>主布局中嵌套其他布局[截图如下]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.demo.customviewclass.TitleViewandroid:id="@+id/title_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.demo.customviewclass.TitleView>
</LinearLayout>
2.2>TitleView类需继承相对布局,同时使用LayoutInflater.inflate()加载TitleView类所对应的布局
3>完整源码: 点击打开链接
2.第二种方式:自绘控件
1>定义:即自绘控件的内容都是自己绘制出来的,在View的onDraw()中完成绘制
2>思路:
2.1>主布局中嵌套其他布局[代码如下]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.wayne.customviewclass2.CounterView
android:id="@+id/title_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.wayne.customviewclass2.CounterView>
</LinearLayout>
2.2>CounterView类继承View,复写View中的onDraw()
3>完整源码: 点击打开链接
3.第三种方式:继承View,进行扩展(未实现)