1.自定義一個組合控件 定義一個累 繼承LinearLayout RelativeLayout
2.重寫構造方法 兩個參數的構造方法 在定義xml佈局文件的時候 回去調用這個構造方法
3.在構造方法裏面 View.inflate(context,R.layout.ui_setting_view,this)把佈局文件
轉換成view對象,掛載到自己身上;
4.爲了方便起見 希望增加一些自定義的屬性;
6.使用自定義的屬性
xmlns:xxxxx="http://schemas.android.com/apk/res/com.itheima.mobilesafe
xxxxx 這個可以隨便寫
res後面是你的應用程序的包名:com.itheima.mobilesafe
7.佈局文件
8.系統解析佈局文件的自定義view對象的時候 ,會調用兩個參數的構造方法
AttributeSet attrs
9.context.obtainStyledAttributes(attrs,R.styleable.setting_View);
讓系統解析到的屬性集合和我們自己定義的集合建立一個對應關係;
10.title = a.getString(R.styleable.setting_View_title);
11.把獲取到的數據設置到界面上;
12.釋放資源 a.recycle();
2.重寫構造方法 兩個參數的構造方法 在定義xml佈局文件的時候 回去調用這個構造方法
3.在構造方法裏面 View.inflate(context,R.layout.ui_setting_view,this)把佈局文件
轉換成view對象,掛載到自己身上;
4.爲了方便起見 希望增加一些自定義的屬性;
5.來到alues目錄, 聲明一個自定義的屬性集
<declare-styleable name="setting_View">
<attr name="title" format="string">
</attr></declare-styleable>
在裏面定義一些自定義的屬性 attr自動在R文件中創建聲明...6.使用自定義的屬性
xmlns:xxxxx="http://schemas.android.com/apk/res/com.itheima.mobilesafe
xxxxx 這個可以隨便寫
res後面是你的應用程序的包名:com.itheima.mobilesafe
7.佈局文件
xxxxx:content_off="我是xxxxxx"
xxxxx:title="我是xxxx"8.系統解析佈局文件的自定義view對象的時候 ,會調用兩個參數的構造方法
AttributeSet attrs
9.context.obtainStyledAttributes(attrs,R.styleable.setting_View);
讓系統解析到的屬性集合和我們自己定義的集合建立一個對應關係;
10.title = a.getString(R.styleable.setting_View_title);
11.把獲取到的數據設置到界面上;
12.釋放資源 a.recycle();