Android -- TypedArray

來源:互聯網
上載者:User

標籤:

當我們自訂View的時候,在給View賦值一些長度寬度的時候,一般都是在layout布局檔案中進行的。,比如android:layout_height="wrap_content",除此之外,我們也可以自己定義屬性,這樣在使用的時候我們就可以使用形如 myapp:myTextSize="20sp"的方式了。

values/attrs.xml

首先要建立變數,建立了個values/attrs.xml檔案,或檔案名稱任意,但是要在values目錄下:

<?xml version="1.0" encoding="utf-8"?>   <resources>       <declare-styleable name="MyView">           <attr name="textSize" format="dimension" />       </declare-styleable>   </resources>

其中resource是跟標籤,可以在裡面定義若干個declare-styleable,<declare-styleable name="MyView">中name定義了變數的名稱,下面可以再自訂多個屬性,針對<attr name="textSize" format="dimension"/>來說,其屬性的名稱為"textSize",format指定了該屬性類型為dimension,只能表示字型的大小。

  • format還可以指定其他的類型比如:
  • reference   表示引用,參考某一資源ID
  • string   表示字串
  • color   表示顏色值
  • dimension   表示尺寸值
  • boolean   表示布爾值
  • integer   表示整型值
  • float   表示浮點值
  • fraction   表示百分數
  • enum   表示枚舉值
  • flag   表示位元運算
layout
<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" >      <us.yydcdut.MyView          android:layout_height="wrap_content"          android:layout_width="wrap_content"          myapp:textSize="20sp"          myapp:myColor="#324243"/>    </LinearLayout>

可以看到多了xmlns:myapp="http://schemas.android.com/apk/res/com.eyu.attrtextdemo" ,以及在自訂View中 myapp:textSize="20sp" ,myapp:myColor="#324243" 

obtainStyledAttributes

context通過調用obtainStyledAttributes方法來擷取一個TypeArray,然後由該TypeArray來對屬性進行設定

obtainStyledAttributes方法有三個,我們最常用的是有一個參數的obtainStyledAttributes(int[] attrs),其參數直接styleable中獲得

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);

調用結束後務必調用recycle()方法,否則這次的設定會對下次的使用造成影響  

public class MyView extends View{      public Paint paint;        public MyView(Context context, AttributeSet attrs) {          super(context, attrs);          paint = new Paint();                    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);      }            @Override     protected void onDraw(Canvas canvas) {          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.