自定义属性

自定义属性的步骤:
1.首先使用在res目录下的attr.xml文件中,新建如下属性集合

<declare-styleable name="CustomProgerssBar3">
        <attr name="roundColor" format="color"/>
        <attr name="roundProgressColor" format="color"/>
        <attr name="roundWidth" format="dimension"/>
        <attr name="textSize" format="dimension"/>
        <attr name="textColor" format="color"/>
        <attr name="max" format="integer"/>
        <attr name="textShow" format="boolean"/>
        <attr name="style" format="enum">
            <enum name="STROKE" value="0"/>
            <enum name="FILL" value="1"/>
        </attr>
    </declare-styleable>
<declare-styleable name="CustomProgerssBar3">

表示一个属性集合,名称叫CustomProgerssBar3,这个属性集合中包括的属性用这种方式定义
关于format的类型有如下几种:
1.color 表示颜色,取值是一个颜色值

<attr name="textColor" format="color"/> 

属性使用示例:

	<Button
        android:layout_width="60dp"
        android:layout_height="60dp"
        app:textColor="@color/colorPrimaryDark"/>

2.dimension 表示尺寸大小,取值是一个数字

<attr name="roundWidth" format="dimension"/>

属性使用示例:

	<Button
        android:layout_width="60dp"
        android:layout_height="60dp"
        app:roundWidth="5dp"/>

3.integer 表示取值是一个整数

<attr name="max" format="integer"/>

属性使用示例:

	<Button
        android:layout_width="60dp"
        android:layout_height="60dp"
        app:max="360"/>

4.boolean 表示取值是一个布尔类型的值

<attr name="textShow" format="boolean"/>

属性使用示例:

	<Button 
        android:layout_width="60dp"
	    android:layout_height="60dp"
	    app:textShow="true"/>

5.enum 表示取值是一个枚举,里面用表示一种枚举类型

      <attr name="style" format="enum">
            <enum name="STROKE" value="0"/>
            <enum name="FILL" value="1"/>
      </attr>

属性使用示例:

	<Button 
	    android:layout_width="60dp"
	    android:layout_height="60dp"
	    app:style="STROKE"/>

6.reference 表示取值是一个引用类型

<attr name="background" format="reference"/>

属性使用示例:

	<ImageView 
        android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		app:background="@drawable/ic_launcher"/>

7.string 表示取值是一个字符串类型

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

属性使用示例:

	<Button 
		android:layout_width="60dp"
		android:layout_height="60dp"
		app:apiKey="6HAja99u0oD1JLGXCRiS2CG"/>

8.float 表示取值是一个浮点类型

<attr name = "fromDegrees" format = "float" />

属性使用示例:

 <Button 
	android:layout_width="60dp"
	android:layout_height="60dp"
	app:fromDegrees="30.0"/>

9.fraction 表示取值一个百分比

<attr name = "pivotX" format = "fraction" />

属性使用示例:

 	 <Button 
		android:layout_width="60dp"
		android:layout_height="60dp"
		app:pivotX="200%"/>

10.flag 表示取值可以是位或`

       <attr name="windowSoftInputMode"> 
        <flag name = "stateHidden" value = "2" /> 
        <flag name = "stateAlwaysHidden" value = "3" /> 
        <flag name = "stateVisible" value = "4" /> 
        <flag name = "stateAlwaysVisible" value = "5" /> 
        <flag name = "adjustUnspecified" value = "0x00" /> 
       </attr>

属性使用示例:

<activity
        android:name = "MainActivity" 
        android:label = "@string/app_name" 
        android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden"> 
		<intent-filter> 
		    <action android:name = "android.intent.action.MAIN" /> 
		    <category android:name = "android.intent.category.LAUNCHER" /> 
		</intent-filter> 
	</activity>

注意:属性定义还能指定多种类型的值

<attr name = "background" format = "reference|color" />

使用示例:

<ImageView
    android:layout_width = "wrap_content" 
    android:layout_height = "wrap_content" 
    android:background = "@drawable/ic_launcher|#00FF00"/>

下面已一个具体的示例来演示如何使用自定义的属性,以及如何获取自定义的属性值。
1.在布局文件activity_custom_progress_bar.xml文件中这样使用自定义的属性

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <test.cn.example.com.androidskill.ui.compact.CustomProgerssBar3
        android:id="@+id/cpb1"
        android:layout_width="60dp"
        android:layout_height="60dp"
        app:textSize="12sp"
        app:textColor="@color/colorPrimaryDark"
        app:roundColor="@color/accent"
        app:roundWidth="5dp"
        app:textShow="true"
        app:roundProgressColor="@color/colorPrimaryDark"/>

    <test.cn.example.com.androidskill.ui.compact.CustomProgerssBar3
        android:id="@+id/cpb2"
        android:layout_marginTop="@dimen/dp16"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:textSize="12sp"
        app:textColor="@color/accent"
        app:roundColor="@color/red"
        app:roundWidth="15dp"
        app:textShow="true"
        app:roundProgressColor="@color/accent"/>


    <test.cn.example.com.androidskill.ui.compact.CustomProgerssBar3
        android:id="@+id/cpb3"
        android:layout_marginTop="@dimen/dp16"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:textSize="12sp"
        app:textColor="@color/accent"
        app:roundColor="@color/red"
        app:roundWidth="15dp"
        app:style="FILL"
        app:textShow="true"
        app:roundProgressColor="@color/accent"/>

    <test.cn.example.com.androidskill.ui.compact.CustomProgerssBar3
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dp16"
        app:textSize="12sp"
        app:textColor="@color/colorPrimaryDark"
        app:roundColor="@color/accent"
        app:roundWidth="5dp"
        app:textShow="true"
        app:roundProgressColor="@color/colorPrimaryDark"/>
</LinearLayout>

2.在自定义CustomProgerssBar3控件中,获取自定义的属性值

public class CustomProgerssBar3 extends View {

    private final int roundColor;
    private final int roundProgressColor;
    private final float roundWidth;
    private final float textSize;
    private final int textColr;
    private final boolean textShow;
    private final int STROKE = 0;
    private final int FILL = 1;
    private final int style;
    private final int max;
    private int progress;
    private final Paint mPaint;
    private int defaultWidth = 0;
    private int defaultHeight = 0;

    public CustomProgerssBar3(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomProgerssBar3);
        roundColor = typedArray.getColor(R.styleable.CustomProgerssBar3_roundColor, Color.CYAN);
        roundProgressColor = typedArray.getColor(R.styleable.CustomProgerssBar3_roundProgressColor, Color.RED);
        roundWidth = typedArray.getDimension(R.styleable.CustomProgerssBar3_roundWidth, 55);
        textSize = typedArray.getDimension(R.styleable.CustomProgerssBar3_textSize, 10);
        textColr = typedArray.getColor(R.styleable.CustomProgerssBar3_textColor, Color.BLUE);
        textShow = typedArray.getBoolean(R.styleable.CustomProgerssBar3_textShow, true);
        style = typedArray.getInt(R.styleable.CustomProgerssBar3_style, STROKE);
        max = typedArray.getInt(R.styleable.CustomProgerssBar3_max, 100);
        mPaint = new Paint();
        LogUtil.i("roundWidth=  "+roundWidth+"     "+ DensityUtil.px2dip(context,roundWidth));
        typedArray.recycle();
    }
}

通过以上两个步骤,就完成了自定义属性的使用,以及自定义属性值的获取,下一篇,将演示,如何自定义一个CustomProgressBar3这个自定义的view

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值