淺析Android手機衛(wèi)士自定義控件的屬性
推薦閱讀:淺析Android手機衛(wèi)士關(guān)閉自動更新
上一節(jié)完成的自定義組合控件,靈活性不夠,控件的顯示信息上,仿照系統(tǒng)屬性,自定義自己的屬性
上一節(jié)組合控件SettingItemView中有三個控件,分別是TextView大標題,TextView描述,CheckBox復(fù)選框
自定義屬性 tsh:title=”大標題” 和tsh:desc_on=”小標題開啟”,tsh:desc_off=”小標題關(guān)閉”
添加命名空間,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é)點
在布局文件使用的時候,會調(diào)用帶有兩個參數(shù)的構(gòu)造方法
在這個構(gòu)造方法里面,會傳遞一個AttributeSet對象
調(diào)用AttributeSet對象的getAttributeValue()方法,得到屬性值,參數(shù):索引位置,不推薦
調(diào)用AttributeSet對象的getAttributeValue(namespace,name)方法,參數(shù):命名空間,屬性名
調(diào)用TextView對象的setText()方法,直接給設(shè)置進去
描述部分,在setChecked()方法里面,判斷,再設(shè)置
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(); } /** * 設(shè)置是否選中 * @param status */ public void setChecked(boolean status){ if(status){ tv_desc.setText(desc_on); }else{ tv_desc.setText(desc_off); } cb_status.setChecked(status); } /** * 設(shè)置顯示文本 * @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" > <TextView android:layout_width="match_parent" android:layout_height="40dp" android:background="#ccc" android:gravity="center" android:text="設(shè)置中心" android:textSize="20sp" /> <com.qingguow.mobilesafe.ui.SettingItemView tsh:title="設(shè)置自動更新" tsh:desc_on="設(shè)置自動更新開啟" tsh:desc_off="設(shè)置自動更新關(guān)閉" 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)士自定義控件的屬性相關(guān)介紹,希望對大家有所幫助!
- Android編程實現(xiàn)號碼歸屬地查詢的方法
- Android手機號碼歸屬地的查詢
- 淺析Android手機衛(wèi)士關(guān)閉自動更新
- 詳解Android 手機衛(wèi)士設(shè)置向?qū)ы撁?/a>
- 深入淺析Android手機衛(wèi)士保存密碼時進行md5加密
- 淺析Android手機衛(wèi)士sim卡綁定
- 淺析Android手機衛(wèi)士讀取聯(lián)系人
- 淺析Android手機衛(wèi)士保存手機安全號碼
- 淺析Android手機衛(wèi)士接收短信指令執(zhí)行相應(yīng)操作
- 淺析Android手機衛(wèi)士手機定位的原理
- 淺析Android手機衛(wèi)士之手機實現(xiàn)短信指令獲取位置
- 淺析Android 手機衛(wèi)士設(shè)備管理權(quán)限鎖屏
- 淺析Android手機衛(wèi)士之號碼歸屬地查詢
相關(guān)文章
Android連接MySQL數(shù)據(jù)庫詳細教程
在Android應(yīng)用程序中連接 MySQL 數(shù)據(jù)庫可以幫助開發(fā)人員實現(xiàn)更豐富的數(shù)據(jù)管理功能,本教程將介紹如何在Android應(yīng)用程序中使用低版本的MySQL Connector/J驅(qū)動程序來連接MySQL數(shù)據(jù)庫,需要的朋友可以參考下2023-05-05Android應(yīng)用的LinearLayout中嵌套RelativeLayout的布局用法
這篇文章主要介紹了Android應(yīng)用的LinearLayout中嵌套RelativeLayout的布局用法,文后還給出了線性布局中一些組件位置的調(diào)試經(jīng)驗,需要的朋友可以參考下2016-04-04舉例講解Android應(yīng)用中SimpleAdapter簡單適配器的使用
這篇文章主要介紹了Android應(yīng)用中SimpleAdapter簡單適配器的使用例子,SimpleAdapter經(jīng)常在ListView被使用,需要的朋友可以參考下2016-04-04Android GestureDetector實現(xiàn)手勢滑動效果
這篇文章主要為大家詳細介紹了Android GestureDetector實現(xiàn)手勢滑動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05更新至Android Studio4.1后發(fā)現(xiàn)as打不開的解決方法(原因分析)
這篇文章主要介紹了更新至Android Studio4.1后發(fā)現(xiàn)as打不開的解決方案,本文給大家分享問題所在原因給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10Android中RecyclerView的item寬高問題詳解
RecyclerView出現(xiàn)已經(jīng)有一段時間了,相信大家肯定不陌生了,下面這篇文章主要給大家介紹了關(guān)于Android中RecyclerView的item寬高問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08