編輯:關於Android編程
Android系統雖然自帶了很多的組件,但肯定滿足我們個性化的需求,所以我們為了開發方便,需要自定義Android的UI組件,以實現我們個性化的需求。
自定義組合控件的步驟:
1 、自定一個View,需要繼承相對布局,線性布局等ViewGroup的子類。ViewGroup是一個其他控件的容器,能夠乘放各種組件。
2 、實現父類的3個構造方法。一般需要在構造方法裡始化初自定義布局文件。
一個參數構造方法:為new控件使用
兩個參數的造方法:在調用布局文件使用
三個參數的造方法:傳遞帶有樣式的布局文件使用
3 、根據需求,定義一些API方法。
4 、根據需要自定義控件的屬性。可以參考TextView的屬性寫。
5 、自定義命名空間。
xmlns:xxx="http://schemas.android.com/apk/res/<包名>" xxx為為scheam名
6 、自定義我們的屬性, 在res/values/attrs.xml(創建屬性文件)定義屬性
類似:
7 、使用自定義的屬性。
例如:
andy:desc_off="設置自動更新已經關閉"
andy:desc_on="設置自動更新已經開啟"
andy:titles="設置自動更新"
8 、在我們自定義控件的帶兩個參數的構造方法裡面,AttributeSet attrs取出自定屬性值,關聯到自定義布局文件對應的控件。
代碼實例如下:
1 定義一個自定義控件:setting_item_view.xml布局文件
2 在對應的Activity布局文件中調用
3 自定義屬性:res/values/attrs.xml
package com.andy.mobilesafe.ui;
import com.andy.mobilesafe.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* @author Zhang,Tianyou
* @version 2014年11月15日 下午10:22:50
*
* 自定義組合控件 兩個TextView 一個checkbox 一個View
*/
public class SettingItemView extends RelativeLayout {
private CheckBox cb_status;
private TextView tv_title;
private TextView tv_desc;
private String desc_on;
private String desc_off;
/**
* 初始化布局文件
*
* @param context
*/
private void initView(Context context) {
// 第二個為布局文件 root第三個參數為布局文件父類
// 把一個布局文件 View 並加載在SettingItemView
View.inflate(context, R.layout.setting_item_view, this);
// View 已經加載該SettingItemView
cb_status = (CheckBox) this.findViewById(R.id.cb_status);
tv_desc = (TextView) this.findViewById(R.id.tv_desc);
tv_title = (TextView) this.findViewById(R.id.title);
}
public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// 這個是可以傳遞一個樣式 調用
initView(context);
}
/**
* 帶有兩個參數的構造方法 ,布局文件使用的時間調用
*
* @param context
* @param attrs
* 得到屬性值
*/
public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
// 自定義布局使用調用的構造方法 attrs為配置的屬性 布局文件中使用
initView(context);
String titles = attrs.getAttributeValue(
"http://schemas.android.com/apk/res/com.andy.mobilesafe",
"titles");
desc_off = attrs.getAttributeValue(
"http://schemas.android.com/apk/res/com.andy.mobilesafe",
"desc_off");
desc_on = attrs.getAttributeValue(
"http://schemas.android.com/apk/res/com.andy.mobilesafe",
"desc_on");
tv_title.setText(titles);
setDesc(desc_off);
}
public SettingItemView(Context context) {
super(context);
// new 出的時間使用
initView(context);
}
/**
* 校驗組合控件是否有選中
*
* @return
*/
public boolean isChecked() {
return cb_status.isChecked();
}
/**
* 設置組合控件選中
*
* @param checked
*/
public void setChecked(boolean checked) {
if(checked){
setDesc(desc_on);
}else {
setDesc(desc_off);
}
cb_status.setChecked(checked);
}
/**
* 設置組合控件的描述信息
*
* @param text
*/
public void setDesc(String text) {
tv_desc.setText(text);
}
}
小米手環支持其他型號的手機嗎
小米手環支持什麼手機,是不是只有小米手機才能用?那麼到底小米手環支持什麼手機?小米手環支持其他型號的手機嗎?小編這裡就為大家詳細的說明一下小米手環支持其他手
Android開發筆記(一百零九)利用網盤實現雲存儲
網盤存儲個人開發者往往沒有自己的後台服務器,但同時又想獲取app的運行信息,這就要借助於第三方的網絡存儲(也叫網盤、雲盤、微盤等等)。通過讓app自動在網盤上存取文件,可
Android中直播視頻技術探究之---桌面屏幕視頻數據源采集功能分析
一、前言之前介紹了Android直播視頻中一種視頻源數據采集:攝像頭Camera視頻數據采集分析中介紹了利用Camera的回調機制,獲取攝像頭的每一幀數據,然後進行二次處
Android的Widget桌面應用學習
一、Android應用的Widget的介紹介紹:Android應用的Widget是應用程序窗口小部件(Widget)是微型的應用程序視圖,它可以被嵌入到其它應用程序中(比