1. 程式人生 > >Android 自定義屬性

Android 自定義屬性

一、概述

平時大家在看別人寫的程式碼時,可能在一個自定義控制元件的XML中也發現過類似的程式碼:


大家看最後三個屬性:

  1. attrstest:headerHeight="300dp"
  2. attrstest:headerVisibleHeight="100dp"
  3. attrstest:age ="young"
明顯這三個屬性不是系統自帶的,這是自己新增上去的。那怎麼新增自定義的屬性呢?利用XML中的declare-styleable標籤來做。

二、declare-styleable使用方法

下面我們先看如何自定義控制元件屬性,然後再講講它的具體用途。


1、自定義一個類MyTextView

  1. publicclass MyTextView extends TextView {  
  2.     public MyTextView(Context context) {  
  3.         super(context);  
  4.     }  
  5. }  

2、新建attrs.xml檔案(res/values下)

複製下面這段程式碼到attrs.xml檔案中:
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3.     <
    declare-styleablename="MyTextView">
  4.         <attrname="header"format="reference"/>
  5.         <attrname="headerHeight"format="dimension"/>
  6.         <attrname="headerVisibleHeight"format="dimension"/>
  7.         <attrname="age">
  8.             <flagname="child"value="10"/>
  9.             <
    flagname="young"value="18"/>
  10.             <flagname="old"value="60"/>
  11.         </attr>
  12.     </declare-styleable>
  13. </resources>
注意:
1、最重要的一點是declare-styleable旁邊有一個name屬性,這個name的取值就是對應所定義的類名。即要為哪個類新增自定義的屬性,那這個name屬性的值就是哪個。當然我們這裡要為自定義的MyTextView來新增XML屬性,所以name = "MyTextView";
2、自定義屬性值可以組合使用比如:<attr name="border_color" format="color|reference"/ >;即表示即可以自定義color值比如#ff00ff,也可以利用@color/XXX來引用color.xml中已有的值


有關<declare-styleable / >標籤下各個標籤的用法,我們後面會逐一講解。
這裡就先講講上面的幾個:
  • reference指的是從string.xml、drawable.xml、color.xml等引用過來的值
  • flag是自己定義的,類似於 android:gravity="top"
  • dimension 指的是是從dimension.xml裡引用過來的內容.注意,這裡如果是dp那就會做畫素轉換
他們在使用的時候是這樣的:
  1. <com.harvic.com.trydeclarestyle.MyTextView
  2.      android:layout_width="fill_parent"
  3.      android:layout_height="match_parent"
  4.      attrstest:header="@drawable/pic1"
  5.      attrstest:headerHeight="300dp"
  6.      attrstest:headerVisibleHeight="100dp"
  7.      attrstest:age="young"/>
可以看到header的取值是從其它XML引用過來的;dimension就表示尺寸,直接輸入數字;flag就相當於程式碼裡的常量,比如這裡的young就表示數字18
理解這些程式碼難度不大,下面看看在XML中要怎麼新增這些自定義的屬性

3、XML中使用自定義的屬性

(1)、新增自定義控制元件

我們在一個XML佈局中,比如下面這個activity_main.xml

  1. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:layout_width="match_parent"
  3.     android:layout_height="match_parent">
  4.     <com.harvic.com.trydeclarestyle.MyTextView
  5.         android:layout_width="fill_parent"
  6.         android:layout_height="match_parent"
  7.         attrstest:header="@drawable/pic1"
  8.         attrstest:headerHeight="300dp"
  9.         attrstest:headerVisibleHeight="100dp"
  10.         attrstest:age="young"/>
  11. </RelativeLayout>
如果我們像上面這樣,直接新增自定義的控制元件及屬性,你會發現,所有的自定義屬性都會標紅!這是因為這個XML根本識別不了這些標記
(2)、匯入自定義的屬性集(方法一)

要讓它識別我們自定義的屬性也非常簡單,在根佈局上新增

  1. xmlns:attrstest ="http://schemas.android.com/apk/res/com.harvic.com.trydeclarestyle"
這裡有兩點注意:
1、xmlns:attrstest,這裡的attrstest是自定義的,你想定義成什麼就可以定義成什麼。但要注意的是,下面訪問你定義的XML控制元件屬性時,就是通過這個識別符號訪問的。比如我們這裡定義成attrstest;那我們下面對應的訪問自定義控制元件的方式就是:attrstest:headerHeight="300dp";
2、最後的:com.harvic.com.trydeclarestyle,是AndroidManifest.xml中的包名。即AndroidManifest.xml中package欄位對應的值,如下所示:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3.     package="com.harvic.com.trydeclarestyle">
所以,在這種方式下,完整的activity_main..xml程式碼應該是:
  1. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:attrstest ="http://schemas.android.com/apk/res/com.harvic.com.trydeclarestyle"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent">
  5.     <com.harvic.com.trydeclarestyle.MyTextView
  6.         android:layout_width="fill_parent"
  7.         android:layout_height="match_parent"
  8.         attrstest:headerHeight="300dp"
  9.         attrstest:headerVisibleHeight="100dp"
  10.         attrstest:age="young"/>
  11. </RelativeLayout>
(3)、匯入自定義的屬性集(方法二)
另一種自動匯入自定義屬性集的方式要相對簡單,要根佈局控制元件上新增:
  1. xmlns:attrstest="http://schemas.android.com/apk/res-auto"
其中xmlns:attrstest,這裡的attrstest是自定義的,你想定義成什麼就可以定義成什麼。但要注意的是,下面訪問你定義的XML控制元件屬性時,就是通過這個識別符號訪問的。比如我們這裡定義成attrstest;那我們下面對應的訪問自定義控制元件的方式就是:attrstest:headerHeight="300dp";
所以,在這種方式下,完整的activity_main.xml的程式碼如下:
  1. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:attrstest="http://schemas.android.com/apk/res-auto"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent">
  5.     <com.harvic.com.trydeclarestyle.MyTextView
  6. 相關推薦

    Android定義屬性時format選項引數說明及用法

    Android自定義屬性時format選項可以取用的值1. reference:參考某一資源ID。 (1)屬性定義:[html] view plaincopyprint?<declare-styleable name="名稱"> <attr

    Android定義屬性,format詳解

      1. reference:參考某一資源ID。     (1)屬性定義:             <declare-styleable name = "名稱">                    <attr name = "background" f

    android定義屬性attrs

    1.定義attrs.xml <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyView"> <a

    Android 定義屬性

    一、概述 平時大家在看別人寫的程式碼時,可能在一個自定義控制元件的XML中也發現過類似的程式碼: 大家看最後三個屬性: attrstest:headerHeight="300dp" attrstest:heade

    Android定義屬性及xmlns解析

    寫在前面 halo,大家好~又到了每週一次的分享時間(放屁!),每天從出門到上班大約有四十多分鐘的公交,也利用這段時間刷刷別人的技術部落格,成長了很多,這周重新溫習了一下Android自定義View和屬性,在這個過程中,對下列xml佈局中的語法產生了好奇:

    android -------- Data Binding的使用 ( 六) 定義屬性

    fan from adapt rim wrap width void 我只 ram 今天來說說DataBinding在自定義屬性的使用 默認的android命名空間下,我們會發現並不是所有的屬性都能直接通過data binding進行設置,比如margin,padding,

    Android原生繪圖進度條+簡單定義屬性程式碼生成器

    零、前言 1.感覺切拼字串是個很有意思的事,好的拼接方式可以自動生成一些很實用的東西 2.本文自定義控制元件並不是很高大上的東西,目的在於計錄自定義控制元件的書寫規範與行文流程 3.建議大家自定義控制元件時自定義屬性有自己專屬字首,有利無害,何樂不為 4.本文是根據鴻洋在慕課網上的教程敲的:詳見,自己

    Android 定義柱狀圖及屬性動畫

    前段時間公司專案中用到了統計圖,網上找了些資料和框架都不能滿足我的需求,沒辦法,只有自己寫了。 近來清閒,將其抽出一個demo了,歡迎大家交流指正。 效果圖先行 實現方案有兩個,一是自定義控制元件,二是使用屬性動畫。屬性動畫在api11以上版本才有,在11版本以下使用可以引入nin

    android 定義註解 通過反射獲取註解屬性

    參考文章:http://xuwenjin666.iteye.com/blog/1637247 1.自定義註解 package cn.veji.hibernate.po; import java.lang.annotation.ElementType; import ja

    Android定義View之定義屬性

    在Android開發中經常會用到自定義View的情況,而在自定義View時,自定義屬性是必須用到的。 1、新建一個自定義View如CustomView 它的自定義屬性主要是通過declare-styleable標籤為其配置自定義屬性。具體做法是:在res/values/目錄下增加一個reso

    HenCoder Android 定義 View 1-7:屬性動畫 Property Animation(進階篇)

    這期是 HenCoder 自定義繪製的第 1-7 期:屬性動畫(進階篇) 簡介 上期的內容,對於大多數簡單的屬性動畫場景已經夠用了。這期的內容主要針對兩個方面: 針對特殊型別的屬性來做屬性動畫; 針對複雜的屬性關係來做屬性動畫。 TypeEvaluator

    Android中XML的名稱空間、定義屬性

    基本概念介紹 名稱空間(namespace) XML 名稱空間提供避免元素命名衝突的方法。 打個比方,A學校有名學生叫做林小明,B學校也有名學生叫林小明,那我們如何識別這兩名擁有相同名字的同學呢?這時候名稱空間就派上用場了。A和B此時就可以被當成是名稱空間了。也就是說,

    Android解決style檔案不能使用定義屬性

    在自定義view的時候,通常會自定義一些屬性,為了便於統一使用,在style檔案中把自定義屬性賦值。但是我卻在自定義view中,取不到style中設定的值,如果在xml中設定屬性值卻能正常獲取,這是為什麼呢? 在res/attrs中自定義屬性attrs.xml: <

    Android定義控制元件屬性(草稿版)

        常用的format型別: 1) string:字串型別; 2) integer:整數型別; 3) float:浮點型; 4) dimension:尺寸,後面必須跟dp、dip、px、sp等單位; 5) Boolean:布林值; 6) refer

    Android 深入理解Android中的定義屬性

    1、引言 對於自定義屬性,大家肯定都不陌生,遵循以下幾步,就可以實現: 自定義一個CustomView(extends View )類 編寫values/attrs.xml,在其中編寫styleable和item等標籤元素 在佈局檔案中CustomView使用自定義的屬性(

    Android定義控制元件 --- 定義屬性 列舉值(固定屬性值)

    今天寫一個自定義控制元件,為了提高使用者使用效率,需要對一個屬性的所有可能屬性值進行列舉(即,只能選擇使用給出的屬性值) 查了很多資料,自己總結一下。 如何寫自定義控制元件就不在贅述了,網上很多大神寫的都很好,此處只說明這一種情況。 attrs.xml <?xml

    Android -- 定義ViewGroup+貝塞爾+屬性動畫實現仿QQ點贊效果

    private void init(final Context context) { mStarDrawable = new ArrayList<>(); mInterpolators = new ArrayList<>(); mSt

    Android Studio 下使用定義屬性注意點

    1.Eclipse 下使用自定義屬性 先要在res 下的value目錄下生成attr.xml中定義自己的屬性,例如下面 <?xml version="1.0" encoding="utf-8"?> <resources>

    Android定義可移動懸浮窗,WindowManager.LayoutParams一些屬性介紹

    效果圖 thanks 首先介紹一下常見的WindowManager.LayoutParams常量屬性 layoutParams.flag int型別 常量介紹 FLAGS_CHANGED 用於表示fl

    從零開始學Android定義View之動畫系列——屬性動畫(3)

    屬性動畫對補間動畫進行了很大幅度的改進,之前補間動畫可以做到的屬性動畫也能做到,補間動畫做不到的現在屬性動畫也可以做到了。因此,今天我們就來學習一下屬性動畫的高階用法,看看如何實現一些補間動畫所無法實現的功能。 ValueAnimator的高階用法 補間