Android 自定义组件相关

自定义组件时,为了组件的通用性,通常采取外部传值的方式改变组件,可以在xml定义时设置,可以用setValue()方式设置,本文讲述的是在xml中设置attr值。

第一种方式:

TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);

1、在valus文件夹中新建一个att.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <declare-styleable name="RoundProgressBar">  
        <attr name="roundColor" format="color"/>
        <attr name="roundProgressColor" format="color"/>
        <attr name="roundWidth" format="dimension"></attr>
        <attr name="textColor" format="color" />  
        <attr name="textSize" format="dimension" /> 
        <attr name="max" format="integer"></attr> 
        <attr name="textIsDisplayable" format="boolean"></attr>
        <attr name="style">
            <enum name="STROKE" value="0"></enum>
            <enum name="FILL" value="1"></enum>
        </attr>
    </declare-styleable> 
</resources>

属性的类型即format有string , integer , dimension , reference , color , enum.,如果可以同时使用不同的类型。用”|“分割。

枚举类型的设置略有不同

 <attr name="flowDirection" format="enum">
            <enum name="leftToRight" value="0" />
            <enum name="topDown" value="1" />
            <enum name="rightToLeft" value="2" />
            <enum name="bottomUp" value="3" />
 </attr>

2、创建自定义组件文件CustomView.java

public class CustomView extends View {
	/**
	 * 画笔对象的引用
	 */
	private Paint paint;

	/**
	 * 圆环的颜色
	 */
	private int roundColor;

	/**
	 * 圆环进度的颜色
	 */
	private int roundProgressColor;

	/**
	 * 中间进度百分比的字符串的颜色
	 */
	private int textColor;

	/**
	 * 中间进度百分比的字符串的字体
	 */
	private float textSize;

	/**
	 * 圆环的宽度
	 */
	private float roundWidth;
	
	/**
	 * 进度的风格,实心或者空心
	 */
	private int style;

	public static final int STROKE = 0;
	public static final int FILL = 1;

	public CustomView(Context context) {
		this(context, null);
	}

	public CustomView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public CustomView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		paint = new Paint();

		TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);

		// 获取自定义属性和默认值
		roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.rgb(238, 238, 238));
		roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);
		textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);
		textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);
		roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);
		max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);	
		style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);

		mTypedArray.recycle();
	}

}
3、在xml中初始化组件

用此方式时,注意xml定义命名空间时必须是”xmlns:“+customName+"http://schemas.android.com/apk/res/"+application包名,即该app的R.java位置

但当project主动引用另外一个android project的时候,即设置成 is library时,用上面方式会出现一个问题,在当前的project中报了library中的一些错误

修改成 :”xmlns:“+customName+"http://schemas.android.com/apk/res-auto

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:android_custom="http://schemas.android.com/apk/res/com.example.circlepregress"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.roundprogressbar.RoundProgressBar
        android:id="@+id/roundProgressBar2"
        android:layout_width="80dip"
        android:layout_height="80dip"
        android:layout_alignLeft="@+id/roundProgressBar1"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="78dp"
        android_custom:roundColor="#D1D1D1"
        android_custom:roundProgressColor="@android:color/black"
        android_custom:roundWidth="10dip"
        android_custom:textColor="#9A32CD"
        android_custom:textSize="18sp" />

</RelativeLayout>

第二种方式:

<strong>attrs.getAttributeIntValue(LOCAL_NS, "radius", 0);//获取对应值</strong>
1、创建自定义组件文件CustomView.java

public class CustomView extends View {
        public static final String GLOBAL_NS = "http://schemas.android.com/apk/res/android";//用于获取android 原有的属性
	private static final String LOCAL_NS = "http://schemas.custom.com";
	private int radius = 0;

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

	public CustomView(Context context, AttributeSet attrs) {
		super(context, attrs);
                String scaleType = attrs.getAttributeValue(GLOBAL_NS, "scaleType");//该属性为android 原有
                if(scaleType == null || scaleType.trim().length() == 0) {
                setScaleType(ScaleType.CENTER_INSIDE);
                }
		radius = attrs.getAttributeIntValue(LOCAL_NS, "radius", 0);
		LogUtil.i("radius=" + radius);
	}

	public CustomCircle(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs);
	}

}

 2、在xml中初始化该组件 

注意:在此方式中,xmlns:custom="http://schemas.cutom.com"必须与自定义组件中的LOCAL_NS一致,但LOCAL_NS不限格式,只要是字符串即可

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.custom.com"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.ulex.ulextest.view.CustomView
        android:id="@+id/customcircle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        custom:radius="103355220" />
</LinearLayout>

批注:若要将自定义组件打包成jar包,可考虑使用第二种方式扩展组件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值