Android 手機衛士--自定義屬性

来源:http://www.cnblogs.com/wuyudong/archive/2016/10/07/5936016.html
-Advertisement-
Play Games

在前面的文章中,已經實現了“設置中心”第一欄的功能以及佈局 本文地址:http://www.cnblogs.com/wuyudong/p/5936016.html,轉載請註明出處。 自定義屬性聲明 接下來實現其他欄的佈局和功能,由於它們之間的功能和佈局類似,只是屬性名稱不同。所以本文在自定義控制項的基 ...


在前面的文章中,已經實現了“設置中心”第一欄的功能以及佈局

本文地址:http://www.cnblogs.com/wuyudong/p/5936016.html,轉載請註明出處。

自定義屬性聲明

接下來實現其他欄的佈局和功能,由於它們之間的功能和佈局類似,只是屬性名稱不同。所以本文在自定義控制項的基礎上實現自定義屬性

首先參考標準控制項的源碼,這裡選擇TextView

源碼路徑為:D:\adt-bundle-windows-x86_64_20140101\sdk\platforms\android-18\data\res\values

打開本文件夾下的attrs.xml文件,找到下麵的代碼:

    <declare-styleable name="TextView">
        <!-- Determines the minimum type that getText() will return.
             The default is "normal".
             Note that EditText and LogTextBox always return Editable,
             even if you specify something less powerful here. -->
        <attr name="bufferType">
            <!-- Can return any CharSequence, possibly a
             Spanned one if the source text was Spanned. -->
            <enum name="normal" value="0" />
            <!-- Can only return Spannable. -->
            <enum name="spannable" value="1" />
            <!-- Can only return Spannable and Editable. -->
            <enum name="editable" value="2" />
        </attr>
        <!-- Text to display. -->
        <attr name="text" format="string" localization="suggested" />
        <!-- Hint text to display when the text is empty. -->
        <attr name="hint" format="string" />

於是我們也可以模仿關鍵節點,寫出自定義屬性,工程res\values文件夾下新建attrs.xml文件,添加代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="com.wuyudong.mobilesafe.view.SettingItemView">
        <attr name="destitle" format="string"/>
        <attr name="desoff" format="string"/>
        <attr name="deson" format="string"/>
    </declare-styleable>
</resources>

構造方法中獲取自定義屬性值

接下來定義命名空間,也是參考android標準來寫

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
……

mobilesafe替換掉原有android

com.wuyudong.mobilesafe必須這樣編寫,替換掉了android,代表當前應用自定義屬性
xmlns:mobilesafe="http://schemas.android.com/apk/res/com.wuyudong.mobilesafe"

修改後的代碼如下:

    <com.wuyudong.mobilesafe.view.SettingItemView
        xmlns:mobilesafe="http://schemas.android.com/apk/res/com.wuyudong.mobilesafe"
        android:id="@+id/siv_update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        mobilesafe:destitle="自動更新設置"
        mobilesafe:desoff="自動更新已關閉"
        mobilesafe:deson="自動更新已開啟" >
    </com.wuyudong.mobilesafe.view.SettingItemView>

自定義屬性值已經搞定,現在是SettingItemView類如何獲取這些值?

在SettingItemView類的構造函數中調用initAttrs函數,然後通過initAttrs函數實現屬性的返回。先通過一些小的實踐來熟悉相關的API

   /**
     * 返回屬性集合中自定義屬性的屬性值
     * @param attrs  構造方法中維護好的屬性集合
     */
    private void initAttrs(AttributeSet attrs) {
        //獲取屬性的總個數
        Log.i(tag,"attrs.getAttributeCount(): " + attrs.getAttributeCount());
        //獲取屬性名稱以及屬性值
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            Log.i(tag, "name = " + attrs.getAttributeName(i));
            Log.i(tag, "value = " + attrs.getAttributeValue(i));
            Log.i(tag, "==================分割線======================");
        }
    }

運行項目後在Logcat中列印下麵的日誌信息:

解釋一下上面的部分代碼:

value = @2131427413對應的十六進位值為:7F0B0055,其實就是對應的R文件中

public static final int siv_update=0x7f0b0055;

其他的都很簡單

接著我們使用其他的API來進行獲取屬性的值

private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.wuyudong.mobilesafe";
...........

 /**
     * 返回屬性集合中自定義屬性的屬性值
     * @param attrs  構造方法中維護好的屬性集合
     */
    private void initAttrs(AttributeSet attrs) {
     /*   //獲取屬性的總個數
        Log.i(tag,"attrs.getAttributeCount(): "+attrs.getAttributeCount());
        //獲取屬性名稱以及屬性值
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            Log.i(tag, "name = " + attrs.getAttributeName(i));
            Log.i(tag, "value = " + attrs.getAttributeValue(i));
            Log.i(tag, "==================分割線======================");
        }*/
        String destitle = attrs.getAttributeValue(NAMESPACE, "destitle");
        String desoff = attrs.getAttributeValue(NAMESPACE, "desoff");
        String deson = attrs.getAttributeValue(NAMESPACE, "deson");
        Log.i(tag, destitle);
        Log.i(tag, desoff);
        Log.i(tag, deson);
    }

運行項目後在Logcat中列印下麵的日誌信息:

說明已經成功獲取所設置的屬性值

這樣就可以復用代碼實現第二欄的電話歸屬地的佈局

    <com.wuyudong.mobilesafe.view.SettingItemView
        xmlns:mobilesafe="http://schemas.android.com/apk/res/com.wuyudong.mobilesafe"
        android:id="@+id/siv_update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        mobilesafe:destitle="自動更新設置"
        mobilesafe:desoff="自動更新已關閉"
        mobilesafe:deson="自動更新已開啟" >
    </com.wuyudong.mobilesafe.view.SettingItemView>

    <com.wuyudong.mobilesafe.view.SettingItemView
        xmlns:mobilesafe="http://schemas.android.com/apk/res/com.wuyudong.mobilesafe"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        mobilesafe:destitle="電話歸屬地的顯示設置"
        mobilesafe:desoff="歸屬地的顯示已關閉"
        mobilesafe:deson="歸屬地的顯示已開啟" >
    </com.wuyudong.mobilesafe.view.SettingItemView>

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 做App測試時監測使用期間的cpu,記憶體,流量,電量等指標時,發現的企鵝很好用的工具。 備份至此,方便後期查閱 以下內容摘抄自企鵝GT官網 http://code.tencent.com/gt.html http://gt.qq.com/ 文檔 http://gt.qq.com/docs.html ...
  • 開發第一應用 可以開發屬於自己的應用,是否有點小激動?好吧!讓我們開始,首先點擊Start a new Android Studio Project創建工程:接下來需要輸入應用名稱(第一個字母要大寫)、公司域以及指定應用存放目錄,點擊Next按鈕進入下一步: 如果第一個字母不是大寫,會提示:The ...
  • 遍歷可變數組的同時刪除數組元素 獲取系統當前語言 UITableView的Group樣式下頂部空白處理 UITableView的plain樣式下,取消區頭停滯效果 獲取某個view所在的控制器 兩種方法刪除NSUserDefaults所有記錄 列印系統所有已註冊的字體名稱 獲取圖片某一點的顏色 字元 ...
  • 最近半年以來,Android熱補丁技術熱潮繼續爆發,各大公司相繼推出自己的開源框架。Tinker在最近也順利完成了公司的審核,並非常榮幸的成為github.com/Tencent上第一個正式公開的項目。 ...
  • 蘋果在iOS10開放了siriKit介面給第三方應用。目前,QQ已經率先適配了Siri的發消息和打電話功能。這意味著在iOS10中你可以直接告訴Siri讓它幫你發QQ消息和打QQ電話了,聽起來是不是很酷炫? 那麼第三方應用使用Siri的體驗究竟如何?哪些應用可以接入SiriKit?接入SiriKi... ...
  • 原文: http://www.2cto.com/kf/201512/455888.html http://blog.csdn.net/yangqingqo/article/details/48371123 http://inthecheesefactory.com/blog/things-you-n ...
  • 遇到這個問題 網上找到的解決辦法: 方法一:就是上面說的通過計算出來ListView或者GridView中的子列高度和 進行顯示:public void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAda ...
  • self.mManager = [[CMMotionManager alloc]init]; self.mManager.deviceMotionUpdateInterval = 0.5; if (self.mManager.gyroAvailable) { [self.mManager start ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...