Android自訂屬性時TypedArray的使用方法

來源:互聯網
上載者:User

標籤:

有時候android傳統的頁面配置不足以滿足我們的需求,常常需要自己定義view,通常繼承View,然後重寫構造方法以及onDraw等函數,再具體實現自己定義的複雜view。我們知道在給控制項賦屬性時,通常使用的是android系統內建的屬性,比如 android:layout_height="wrap_content",除此之外,我們亦可以自己定義屬性,這樣在使用的時候我們就可以使用形如 myapp:myTextSize="20sp"的方式了,步驟大致如下:1 在專案檔res/value下面建立一個attr.xml檔案,該檔案中包含若干個attr集合,例如:[html]<?xml version="1.0" encoding="utf-8"?>  <resources>      <declare-styleable name="MyView">          <attr name="myTextSize" format="dimension"/>          <attr name="myColor" format="color"/>      </declare-styleable>  </resources>   其中resource是跟標籤,可以在裡面定義若干個declare-styleable,<declare-styleable name="MyView">中name定義了變數的名稱,下面可以再自訂多個屬性,針對<attr name="myTextSize" format="dimension"/>來說,其屬性的名稱為"myTextSize",format指定了該屬性類型為dimension,只能表示字型的大小。format還可以指定其他的類型比如;reference   表示引用,參考某一資源IDstring   表示字串color   表示顏色值dimension   表示尺寸值boolean   表示布爾值integer   表示整型值float   表示浮點值fraction   表示百分數enum   表示枚舉值flag   表示位元運算 2 在使用到該自訂view的布局檔案中鍵入如下的一行:綠色是自己定義屬性的首碼名字,粉色是項目的包名,這樣一來,在我們自己定義的view的屬性中,就可以使用自己在attr中定義的屬性啦,例如:[html] <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:myapp="http://schemas.android.com/apk/res/com.eyu.attrtextdemo"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:orientation="vertical"      tools:context=".MainActivity" >        <TextView          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="@string/hello_world" />      <com.eyu.attrtextdemo.MyView          android:layout_height="wrap_content"          android:layout_width="wrap_content"          myapp:myTextSize="20sp"          myapp:myColor="#324243"/>    </LinearLayout>     myapp:myColor="#324243" 首碼必須跟 xmlns:myapp="http://schemas.android.com/apk/res/com.eyu.attrtextdemo"  ,  這兩個地方必須一樣,後面這個地址是 http://schemas.android.com/apk/res/ 加上包名

3 在自訂view的代碼中引入自訂屬性,修改建構函式context通過調用obtainStyledAttributes方法來擷取一個TypeArray,然後由該TypeArray來對屬性進行設定obtainStyledAttributes方法有三個,我們最常用的是有一個參數的obtainStyledAttributes(int[] attrs),其參數直接styleable中獲得TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);調用結束後務必調用recycle()方法,否則這次的設定會對下次的使用造成影響  具體如下:[java] package com.eyu.attrtextdemo;    import android.content.Context;  import android.content.res.TypedArray;  import android.graphics.Canvas;  import android.graphics.Paint;  import android.graphics.Paint.Style;  import android.util.AttributeSet;  import android.view.View;    public class MyView extends View{      public Paint paint;        public MyView(Context context, AttributeSet attrs) {          super(context, attrs);          paint = new Paint();            // R.styleable.MyView這個MyView是根據<declare-styleable name="MyView">  而取        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);              int textColor = a.getColor(R.styleable.MyView_myColor, 003344);          float textSize = a.getDimension(R.styleable.MyView_myTextSize, 33);          paint.setTextSize(textSize);          paint.setColor(textColor);          a.recycle();      }        public MyView(Context context) {          super(context);          // TODO Auto-generated constructor stub      }            @Override   www.2cto.com    protected void onDraw(Canvas canvas) {          // TODO Auto-generated method stub          super.onDraw(canvas);             paint.setStyle(Style.FILL);          canvas.drawText("aaaaaaa", 10, 50, paint);      }                    }  

Android自訂屬性時TypedArray的使用方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.