最近很多地方用到這個...詳細寫下
就先不在公司項目裏寫了,暫時在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文件中會自動生成兩個屬性字段
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);
}
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。結束.....