国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

淺析Android手機衛(wèi)士自定義控件的屬性

2019-12-12 06:39:15
字體:
來源:轉載
供稿:網(wǎng)友

推薦閱讀:淺析Android手機衛(wèi)士關閉自動更新

上一節(jié)完成的自定義組合控件,靈活性不夠,控件的顯示信息上,仿照系統(tǒng)屬性,自定義自己的屬性

上一節(jié)組合控件SettingItemView中有三個控件,分別是TextView大標題,TextView描述,CheckBox復選框

自定義屬性 tsh:title=”大標題” 和tsh:desc_on=”小標題開啟”,tsh:desc_off=”小標題關閉”

添加命名空間,xmlns:tsh=”http://schemas.android.com/apk/res/包名"

在res/values/目錄下創(chuàng)建 attrs.xml文件

添加節(jié)點 <declare-styleable name=”TextView”>

節(jié)點下添加節(jié)點<attr name=”title” format=”string”/>,添加其他兩個屬性的節(jié)點

在布局文件使用的時候,會調用帶有兩個參數(shù)的構造方法

在這個構造方法里面,會傳遞一個AttributeSet對象

調用AttributeSet對象的getAttributeValue()方法,得到屬性值,參數(shù):索引位置,不推薦

調用AttributeSet對象的getAttributeValue(namespace,name)方法,參數(shù):命名空間,屬性名

調用TextView對象的setText()方法,直接給設置進去

描述部分,在setChecked()方法里面,判斷,再設置

SettingItemView.java

package com.qingguow.mobilesafe.ui;import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.widget.CheckBox;import android.widget.RelativeLayout;import android.widget.TextView;import com.qingguow.mobilesafe.R;public class SettingItemView extends RelativeLayout {private TextView tv_title;private TextView tv_desc;private CheckBox cb_status;private String desc_on;private String desc_off;/*** 初始化View對象* @param context*/private void initView(Context context) {View.inflate(context, R.layout.setting_item_view, this);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.tv_title);}/*** 判斷是否選中* @return*/public boolean isChecked(){return cb_status.isChecked();}/*** 設置是否選中* @param status*/public void setChecked(boolean status){if(status){tv_desc.setText(desc_on);}else{tv_desc.setText(desc_off);}cb_status.setChecked(status);}/*** 設置顯示文本* @param text*/public void setDesc(String text){tv_desc.setText(text);}public SettingItemView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);initView(context);}public SettingItemView(Context context, AttributeSet attrs) {super(context, attrs);initView(context);     //獲取傳遞的屬性String title=attrs.getAttributeValue("http://schemas.android.com/apk/res/com.qingguow.mobilesafe", "title");tv_title.setText(title);desc_on=attrs.getAttributeValue("http://schemas.android.com/apk/res/com.qingguow.mobilesafe", "desc_on");desc_off=attrs.getAttributeValue("http://schemas.android.com/apk/res/com.qingguow.mobilesafe", "desc_off");}public SettingItemView(Context context) {super(context);initView(context);}}

activity_setting.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tsh="http://schemas.android.com/apk/res/com.qingguow.mobilesafe"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:layout_width="match_parent"android:layout_height="40dp"android:background="#ccc"android:gravity="center"android:text="設置中心"android:textSize="20sp" /><com.qingguow.mobilesafe.ui.SettingItemViewtsh:title="設置自動更新"tsh:desc_on="設置自動更新開啟"tsh:desc_off="設置自動更新關閉"android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/siv_item"></com.qingguow.mobilesafe.ui.SettingItemView></LinearLayout> 

attrs.xml

<?xml version="1.0" encoding="utf-8"?><resources><declare-styleable name="TextView"><attr name="title" format="string" /><attr name="desc_on" format="string" /><attr name="desc_off" format="string" /></declare-styleable></resources>

以上是針對Android手機衛(wèi)士自定義控件的屬性相關介紹,希望對大家有所幫助!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁德市| 会理县| 班玛县| 阿鲁科尔沁旗| 玉林市| 东至县| 洪雅县| 鄱阳县| 大冶市| 长春市| 大连市| 当阳市| 定日县| 巴彦县| 湟源县| 临沧市| 潞西市| 沙洋县| 丰县| 东乡| 丹江口市| 新沂市| 黔西| 永胜县| 吐鲁番市| 湖北省| 赤壁市| 达孜县| 班戈县| 佛坪县| 潮州市| 从化市| 邵东县| 嘉兴市| 上杭县| 抚宁县| 杨浦区| 于都县| 固始县| 桂东县| 阳山县|