Android自定義屬性的使用示例

jopen 11年前發布 | 35K 次閱讀 Android Android開發 移動開發

MainActivity如下:

package cc.testattrs;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


ViewSubclass如下:

package cc.testattrs;

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.util.AttributeSet;
import android.view.View;
/**
 * Demo描述:
 * Android自定義屬性的使用
 * 
 * 注意事項:
 * 1 在main.xml中聲明命名空間
 *   xmlns:testattr="http://schemas.android.com/apk/res/cc.testattrs"
 *   其中http://schemas.android.com/apk/res/為固定寫法,其后追加包名
 *   testattr為我們給自定義屬性的別名引用
 * 2 getDimension(R.styleable.TestAttr_testTextSize, 20);
 *   第二個參數意思是:假如在xml文件中沒有為改屬性設值則采用此值.
 *   其余getXX()方法均類似
 * 3 注意getColor()方法中第二個參數的取值,是一個顏色值,在這里很容易錯誤
 *
 */
public class ViewSubclass extends View {
    private Paint mPaint;
    private float textSize;
    private int textColor ;
    public ViewSubclass(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ViewSubclass(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.TestAttr);
        textSize = typedArray.getDimension(R.styleable.TestAttr_testTextSize, 20);
        textColor = typedArray.getColor(R.styleable.TestAttr_testColor, Color.BLACK);
        System.out.println("textSize="+textSize+",textColor="+textColor);
        mPaint.setTextSize(textSize);
        mPaint.setColor(textColor);
        //切記recycle()
        typedArray.recycle();
    }

    public ViewSubclass(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setStyle(Style.FILL);  
        canvas.drawText("9527", 10, 20, mPaint);
    }

}

 

main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:testattr="http://schemas.android.com/apk/res/cc.testattrs"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
   >

    <cc.testattrs.ViewSubclass
        android:layout_width="200dip"
        android:layout_height="200dip"
        android:layout_centerInParent="true" 
        testattr:testTextSize="10dip"
        testattr:testColor="#ff0000"
    />

</RelativeLayout>

 

attrs.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
      <declare-styleable name="TestAttr">
        <attr name="testTextSize" format="dimension"/>
        <attr name="testColor" format="color"/>
    </declare-styleable>
</resources>




 

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!