android 自定義控件中獲取屬性的三種方式(轉)


第一種方法,直接設置屬性值,通過attrs.getAttributeResourceValue拿到這個屬性值。

(1)在xml文件中設置屬性值

<com.example.activity.IconTextView   
       android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="@string/smile1"  
    iconSrc="@drawable/smile"/>  

(2)在構造函數中拿到這個值

public IconTextView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        resourceID = attrs.getAttributeResourceValue(null, "iconSrc", 0);  
        if(resourceID > 0) {  
            bitmap = BitmapFactory.decodeResource(getResources(), resourceID);  
        }  
    }  

第二種方法,使用自己的命名空間

(1)注意在xml文件中,需要聲明一個命名空間,形式為http:// + 這個VIEW的包名

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:mobile="http://com.example.activity"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="vertical" >  
  
    <com.example.activity.IconTextView   
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="@string/smile1"  
        mobile:iconSrc="@drawable/smile"/>  
  
</LinearLayout>  

(2)通過attrs.getAttributeResourceValue,其中第一個參數為命名空間。

  1. //命名空間  

private final String namespace = "http://com.example.activity" 

 public IconTextView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        resourceID = attrs.getAttributeResourceValue(namespace, "iconSrc", 0);  
//      TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.IconTextView);  
//      resourceID = array.getResourceId(R.styleable.IconTextView_iconSrc, 0);  
        if(resourceID > 0) {  
            bitmap = BitmapFactory.decodeResource(getResources(), resourceID);  
        }  
    }  

第三種方法,通過自定義attrs.xml來實現

(1)自定義一個attrs.xml文件

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <declare-styleable name="IconTextView">  
        <attr name="iconSrc" format="reference"/>  
    </declare-styleable>  
</resources>  

(2)在xml文件中使用這一屬性,注意此時命名空間的書寫規范。

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:mobile="http://schemas.android.com/apk/res/com.example.activity"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="vertical" >  
  
    <com.example.activity.IconTextView   
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="@string/smile1"  
        mobile:iconSrc="@drawable/smile"/>  
      
    <com.example.activity.IconTextView   
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="@string/smile2"  
        android:textSize="24dp"  
        mobile:iconSrc="@drawable/smile"/>  
      
    <com.example.activity.IconTextView   
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="@string/smile3"  
        android:textSize="36dp"  
        mobile:iconSrc="@drawable/smile"/>  
      
  
</LinearLayout>  

(3)在代碼中使用context.obtainStyledAttributes獲得屬性值

package com.example.activity;  
  
import android.content.Context;  
import android.content.res.TypedArray;  
import android.graphics.Bitmap;  
import android.graphics.BitmapFactory;  
import android.graphics.Canvas;  
import android.graphics.Rect;  
import android.util.AttributeSet;  
import android.widget.TextView;  
  
public class IconTextView extends TextView {  
    //命名空間  
    private final String namespace = "http://com.example.activity";  
    //資源ID  
    private int resourceID = 0;  
    private Bitmap bitmap;  
  
    public IconTextView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.IconTextView);  
        resourceID = array.getResourceId(R.styleable.IconTextView_iconSrc, 0);  
        if(resourceID > 0) {  
            bitmap = BitmapFactory.decodeResource(getResources(), resourceID);  
        }  
    }  
      
    @Override  
    public void onDraw(Canvas canvas) {  
        if (bitmap != null) {  
            Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());  
              
            Rect target = new Rect();  
            int textHeight = (int)getTextSize();  
            target.left = 0;  
            target.top =(int)(getMeasuredHeight() - getTextSize()) / 2 + 1;  
            target.bottom = target.top + textHeight;  
            target.right = (int)(textHeight * (bitmap.getWidth() / (float)bitmap.getHeight()));  
              
            canvas.drawBitmap(bitmap, src, target, getPaint());  
              
            canvas.translate(target.right + 2, 0);  
        }  
          
        super.onDraw(canvas);  
    }  
      
}  

 


免責聲明!

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



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