android自定義屬性的使用


最近在學習一個開源的項目,看到人家定義的資源文件有如下標簽:

而在該項目中,利用以上路徑追溯下去,會追溯到這么一個類文件,所以就迷糊了,定義布局文件跟類有毛關系<比較二>查了下

原來是自定義屬性的使用!

------------------------------------------------------------------------------------------------------------------------

先看一張關系圖:

 

其實就是,在values目錄下定義一個attrs.xml,在對應的類文件里生成某些組件,在layout布局文件里為這些屬性賦值

引用牛人一個例子:

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

        <attr name="test1" format="string" />  

    <declare-styleable name="MyView">
              <attr name="textColor" format="color" />  
              <attr name="textSize" format="dimension" />  
              <attr name="text" format="string" />
        </declare-styleable>  
</resources>

解釋:

attr子元素:

定義具體的屬性,format表示這個屬性的值的類型,類型有以下幾種:
     1.reference:參考指定Theme中資源ID,這個類型意思就是你傳的值可以是引用資源
     2.string:字符串,如果你想別人既能直接寫值也可以用類似"@string/test"引用資源的方式,可以寫成format="string|reference"
     3.Color:顏色
     4.boolean:布爾值
     5.dimension:尺寸值
     6.float:浮點型
     7.integer:整型
     8.fraction:百分數
     9.enum:枚舉 ,如果你提供的屬性只能讓別人選擇,不能隨便傳入,就可以寫成這樣
        <attr name="language">
                   <enum name="china" value="1"/>
                   <enum name="English" value="2"/>
            </attr>
     10.flag:位或運算


declare-styleable子元素:

定義一個styleable對象,每個styleable對象就是一組attr屬性的集合 注意:這里的name屬性並不是一定要和自定義類名相同,只是為了好區分對應類的屬性而已


注意:上面的屬性資源文件定義了該屬性之后,至於到底是哪個自定義View組件中來使用該屬性,該屬性到底能發揮什么作用, 就不歸該屬性資源文件管了,也就是說這個屬性資源文件是個公共的,大家都可以用,但是為了方便管理,一般都是一個自定義View里的屬性寫成一個declare-styleable集合.屬性資源所定義的屬性到底可以返回什么作用,取決於自定義組件的代碼實現

3、定義組件類

package cn.com.androidtest.ui;

import cn.com.androidtest.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View
{

     private Paint mPaint;  
     private Context mContext;  
     private static String mString;
     private String test;
       
    public MyView(Context context) 
    {  
          super(context);  
            mPaint = new Paint();  
    }  
    
    public MyView(Context context,AttributeSet attrs)  
    {  
            super(context,attrs);  
            mPaint = new Paint();  
            
                /*這里取得declare-styleable集合*/
             TypedArray typeArray = context.obtainStyledAttributes(attrs,R.styleable.MyView);   
                 /*這里從集合里取出相對應的屬性值,第二參數是如果使用者沒用配置該屬性時所用的默認值*/
            int textColor = typeArray.getColor(R.styleable.MyView_textColor,0XFFFFFFFF);  
            float textSize = typeArray.getDimension(R.styleable.MyView_textSize, 36);  
            mString = typeArray.getString(R.styleable.MyView_text);
             /*設置自己的類成員變量*/
            mPaint.setTextSize(textSize);  
            mPaint.setColor(textColor);  
            /*關閉資源*/
            typeArray.recycle();  
    }  
    @Override  
    protected void onDraw(Canvas canvas) 
    {  
         super.onDraw(canvas);  

         mPaint.setStyle(Style.FILL);        
         canvas.drawRect(new Rect(10, 10, 90, 90), mPaint);        
         mPaint.setColor(Color.BLUE);   
         canvas.drawText(mString, 10, 110, mPaint);  
    }  
}

 

4、布局文件引用賦值

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myandroid="http://schemas.android.com/apk/res/cn.com.androidtest"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"/>
    
    <cn.com.androidtest.ui.MyView
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         myandroid:textColor="#ff0000"
         myandroid:textSize="20px"
         myandroid:text="http://wujiandong.iteye.com"/>
</LinearLayout>

注意:

java代碼里那種取屬性值的方式,那么在XML使用該組件的時候一定要為該自定義組件設置一個命名空間[xmlns:myandroid="http://schemas.android.com/apk/res/cn.com.androidtest"],不然組件屬性設置不了 
命名空間寫法:xmlns:空間名="http://schemas.android.com/apk/res/自定義組件所在包名" 
寫包名時候也有個要注意的地方: 
如果你的自定義View所在包類似如下兩圖,那么包名只能寫成最頂層包[cn.com.androidtest],而不能是[cn.com.androidtest.ui] 

包試圖:

 

詳細進入大牛博客:點擊查看

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM