[android] 手機衛士自訂控制項的屬性,android衛士

來源:互聯網
上載者:User

[android] 手機衛士自訂控制項的屬性,android衛士

上一節完成的自訂群組合控制項,靈活性不夠,控制項的顯示資訊上,仿照系統屬性,自訂自己的屬性

上一節群組控制項SettingItemView中有三個控制項,分別是TextView大標題,TextView描述,CheckBox複選框

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

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

在res/values/目錄下建立 attrs.xml檔案

添加節點 <declare-styleable name=”TextView”>

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

 

在布局檔案使用的時候,會調用帶有兩個參數的構造方法

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

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

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

調用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" >    <TextView        android:layout_width="match_parent"        android:layout_height="40dp"        android:background="#ccc"        android:gravity="center"        android:text="設定中心"        android:textSize="20sp" />    <com.qingguow.mobilesafe.ui.SettingItemView        tsh: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>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.