Android学习笔记(十五)自定义控件(declare-styleable 的使用)

   在实际开发中,系统自带的wdigt常常们不能满足我们对控件的需求。我们就得自己开发自定义的控件。
在源码中我们需要像java类那样去继承(extends)我们要开发的控件父类。而在Android xml文件中的配置实现就需要 使用declare-styleable标签了

自定义控件的一般过程

1、在res/values/目录下创建一个attrs.xml,作为我们自定义控件的属性定义文件
2、编写内容
<resources>
<declare-styleable name="PieChart">
    <attr name="showText" format="boolean" />
    <attr name="labelPosition" format="enum">
        <enum name="left" value="0"/>
        <enum name="right" value="1"/>
    </attr>
</declare-styleable>
</resources>
这里我们定义了一个boolean类型的属性showText和emum类型的属性labelPosition
3、在 layout 文件夹中的定义文件中,在使用处引用 
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews">
 <com.example.customviews.charting.PieChart
     custom:showText="true"
     custom:labelPosition="left" />
</LinearLayout>
4、在代码中获取 layout 文件中的值
public PieChart(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.getTheme().obtainStyledAttributes(
         attrs,
         R.styleable.PieChart,
         0, 0);

    try {
        mShowText = a.getBoolean(R.styleable.PieChart_showText, false);
        mTextPos = a.getInteger(R.styleable.PieChart_labelPosition, 0);
    } finally {
        a.recycle();
    }
 }
这里需要调用recycle()方法对资源进行回收
5、动态修改属性值
    我们自定义的控件就可以从属性集中获取设置,这些属性值从配置中只能读取一次。要想实现动态访问和修改属性值,则需要向外部提供访问和修改的方法。如下:
public boolean isShowText() {
    return mShowText;
 }

 public void setShowText(boolean showText) {
    mShowText = showText;
    invalidate();
    requestLayout();
 }

各种属性类型的自定义简介

一、reference:参考指定Theme中资源ID。

1.定义:

    <declare-styleable name="My">
        <attr name="label" format="reference" >
    </declare-styleable>
2.使用:

 <Buttonzkx:label="@string/label" >

二、Color:颜色

1.定义:

  <declare-styleable name="My">
        <attr name="textColor" format="color" />
    </declare-styleable>
2.使用:

 <Button zkx:textColor="#ff0000"/>

三、boolean:布尔值

1.定义:

  <declare-styleable name="My">
        <attr name="isVisible" format="boolean" />
    </declare-styleable>
2.使用:
 <Button zkx:isVisible="false"/>
四、dimension:尺寸值
1.定义:
    <declare-styleable name="My">
        <attr name="myWidth" format="dimension" />
    </declare-styleable>
2.使用:
  <Button zkx:myWidth="100dip"/>
五、float:浮点型
1.定义:
   <declare-styleable name="My">
        <attr name="fromAlpha" format="float" />
    </declare-styleable>
2.使用:
  <alpha zkx:fromAlpha="0.3"/>

六、integer:整型

1.定义:

 <declare-styleable name="My">
        <attr name="frameDuration" format="integer" />
    </declare-styleable>
2.使用:
    <animated-rotate zkx:framesCount="22"/>
七、string:字符串

1.定义:

    <declare-styleable name="My">
        <attr name="Name" format="string" />
    </declare-styleable>
2.使用:
<rotate zkx:pivotX="200%"/>

八、fraction:百分数

1.定义:

 <declare-styleable name="My">
        <attr name="pivotX" format="fraction" />
    </declare-styleable>
2.使用:
 <rotate zkx:Name="My name is michael"/>

九、enum:枚举

1.定义:

    <declare-styleable name="My">
        <attr name="language">
            <enum name="English" value="1"/>
        </attr>
    </declare-styleable>
2.使用:
<Button zkx:language="English"/>

十、flag:位或运算

1.定义:

   <declare-styleable name="My">
        <attr name="windowSoftInputMode">
    	<flag name="stateUnspecified" value="1" />
    	<flag name = "adjustNothing" value = "0x30" />
        </attr>
    </declare-styleable>
2.使用:
 <activity android:windowSoftInputMode="stateUnspecified | adjustNothing">
属性定义时可以指定多种类型值:
  <declare-styleable name = "名称">    
	<attr name="background" format="reference|color" />
    </declare-styleable>
使用:
<ImageView android:background = "@drawable/图片ID|#00FF00"/>

参考:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值