自定義組合控制項的過程歸納

2021-09-08 15:05:08 字數 2281 閱讀 7652

1.寫乙個類繼承viewgroup,並實現三個構造方法;

2.並且在構造方法裡面初始化布局檔案;

3.根據需求增加api方法

---以上散步自定義組合控制項----

4.為了使用組合控制項更加方便,自定義組合控制項

5.自定義命名空間:

xmlns:自定義名字="包名"

例如:xmlns:itheima="com.example.mobilesafe"

6.自定義屬性,在工程res/values/attrs.xml

例如: ?

0102

03

04

05

06

07

08

09

10

<fontface="黑體">

<resources>

<declare-styleablename="settingitemview">

<attrname="title"format="string">

<attrname="update_on"format="string">

<attrname="update_off"format="string">

7.使用我們自定義好的屬性

例如: ?

1<com.example.mobilesafe.view.settingitemviewandroid:id="@+id/siv_update"android:layout_width="wrap_content"android:layout_height="wrap_content"itheima:title="設定自動更新"itheima:update_off="自動更新已經關閉"itheima:update_on="自動更新已經開啟"><fontface="黑體">

8.布局檔案的屬性和對應的類進行管理,在自定義控制項的帶有兩個引數的構造方法裡面有乙個類

attributeset裡面封裝了布局檔案的屬性,需要把裡面的值取出來,賦值給布局檔案

(1).取值:eg:update_off =attrs.getattributevalue("

/com.example.mobilesafe","update_off");

(2.)賦值:eg:tv_title.settext(title);

自定義組合控制項的過程

自定義組合控制項的過程 1.自定義乙個view 一般來說,繼承相對布局,或者線性布局 viewgroup 2.實現父類的構造方法。一般來說,需要在構造方法裡初始化自定義的布局檔案 3.根據一些需要或者需求,定義一些api方法 4.根據需要,自定義控制項的屬性,可以參照textview屬性 5.自定義...

自定義組合控制項

package com.example.horimagedemo import android.content.context import android.util.attributeset import android.widget.relativelayout public class set...

自定義組合控制項

mainactivitycustomtitlebar import android.content.context import android.util.attributeset import android.view.layoutinflater import android.widget.bu...