当前位置: 首页 > article >正文

TypedArray的用法和declare-styleable:自定义控件的属性

TypedArray 用于存放  

 

android 自定义控件

写在构造方法当中

 

      // 把属性集 和我们自己定义的属性集合建立映射关系
      TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.setting_view_style);       

      String title = a.getString(R.styleable.setting_view_style_title);       

      checked_text = a.getString(R.styleable.setting_view_style_checked_text);       

      unchecked_text = a.getString(R.styleable.setting_view_style_unchecked_text);


        // 获取styleable中的资源 format 
        tv_settingview_content.setText(unchecked_text);
        tv_settingview_title.setText(title);
        a.recycle();// 释放资源.

 

 

在res/value目录下 创建配置文件

 attrs.xml

例1:

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <declare-styleable name="setting_view_style">
         <attr name="title" format="string"></attr>
         <attr name="checked_text" format="string"></attr>
         <attr name="unchecked_text" format="string"></attr>
     </declare-styleable>
</resources>

例2:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="TestAttr">
        <attr name="name" format="reference" />
        <attr name="age">
            <flag name="child" value="10" />
            <flag name="young" value="18" />
            <flag name="oldman" value="60" />
        </attr>
        <attr name="textSize" format="dimension" />  
    </declare-styleable>
</resources>

 

 

 

在XML布局中使用自定义布局时 

需要 书写命名空间

包名是你所在的项目的根包.也就是在manifest里的

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"


    xmlns:aaawqqq=http://schemas.android.com/apk/res/com.aaawqqq.ui


    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

 

包名.类名 

   <com.aaawqqq.ui.SettingView
        android:id="@+id/sv_task_setting_autokill"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        itheima:checked_text="已经开启"
        itheima:title="自动清理内存"
        itheima:unchecked_text="没有开启" / >

 

以下转自

http://blog.csdn.net/congqingbin/article/details/7869730

自定义属性数据类型简介:

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

1.定义:

1
2
3
    <declare-styleable name="My"><attr name="label" format="reference" ></declare-styleable>

2.使用:

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

二、Color:颜色

1.定义:

1
2
3
    <declare-styleable name="My"><attr name="textColor" format="color" /></declare-styleable>

2.使用:

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

三、boolean:布尔值

1.定义:

1
2
3
    <declare-styleable name="My"><attr name="isVisible" format="boolean" /></declare-styleable>

2.使用:

1
    <Button zkx:isVisible="false"/>

四、dimension:尺寸值

1.定义:

1
2
3
    <declare-styleable name="My"><attr name="myWidth" format="dimension" /></declare-styleable>

2.使用:

1
    <Button zkx:myWidth="100dip"/>

五、float:浮点型

1.定义:

1
2
3
    <declare-styleable name="My"><attr name="fromAlpha" format="float" /></declare-styleable>

2.使用:

1
    <alpha zkx:fromAlpha="0.3"/>

六、integer:整型

1.定义:

1
2
3
    <declare-styleable name="My"><attr name="frameDuration" format="integer" /></declare-styleable>

2.使用:

1
    <animated-rotate zkx:framesCount="22"/>

七、string:字符串

1.定义:

1
2
3
    <declare-styleable name="My"><attr name="Name" format="string" /></declare-styleable>

2.使用:

1
    <rotate zkx:pivotX="200%"/>

八、fraction:百分数

1.定义:

1
2
3
    <declare-styleable name="My"><attr name="pivotX" format="fraction" /></declare-styleable>

2.使用:

1
    <rotate zkx:Name="My name is zhang kun xiang"/>

九、enum:枚举

1.定义:

1
2
3
4
5
    <declare-styleable name="My"><attr name="language"><enum name="English" value="1"/></attr></declare-styleable>

2.使用:

1
    <Button zkx:language="English"/>

十、flag:位或运算

1.定义:

1
2
3
4
5
6
    <declare-styleable name="My"><attr name="windowSoftInputMode"><flag name="stateUnspecified" value="1" /><flag name = "adjustNothing" value = "0x30" /></attr></declare-styleable>

2.使用:

1
    <activity android:windowSoftInputMode="stateUnspecified | adjustNothing">

属性定义时可以指定多种类型值:

1
2
3
    <declare-styleable name = "名称">    <attr name="background" format="reference|color" /></declare-styleable>

使用:

1
    <ImageView android:background = "@drawable/图片ID|#00FF00"/>

 

 

http://www.lryc.cn/news/2418986.html

相关文章:

  • 【IO】读写文件(FileInputStream、FileOutputStream、FileReader、FileWriter、RandomAccessFile)
  • 网络信息安全等级与标准_比较tcsec,cc及我国信息安全等级保护标准
  • 斗罗大陆武魂觉醒礼包码最新兑换码2024
  • 五笔字根表
  • (虚拟机)win10忘记密码重置_copy sethc,熬夜整理华为最新网络安全笔试题
  • 用移动硬盘当系统盘,即插即用
  • HTML的基本标签II
  • adns库的使用
  • JS检索下拉列表框中被选项目的索引号(selectedIndex)
  • C#报表控件ReportViewer
  • 权限表设计
  • 如何在本地设置www.xxx.com,使其允许访问
  • 解决MMC无法创建管理单元的问题!
  • 使用小马哥win10 激活工具激活后, 桌面上经常出现一个广告快捷方式的 解决方法...
  • Object-C 与C/C++的区别
  • 在word文档中如何自动生成目录,两种方法制作目录,总有一种适合你
  • 网工请自查!这十个技术问题还不会就out了
  • 使用Python Web(Django)和Bootstrap制作一款精美的“在线计算器”手把手教学——(基于Windows平台开发和部署,附完整代码和演示实例)
  • StretchDIBits函数
  • 人工智能--产生式表示法
  • VC-终止线程,ExitThread函数,TerminateThread函数,撤消线程,详解
  • eclipse-che01 安装eclipseche
  • 计算机网络自学指南,怒推荐一波,简直太全面了!
  • 小米更新显示非官方rom_魔趣ROM 安装刷入教程(小白新手)
  • 多路径软件常用操作(MPIO)
  • C语言学生成绩信息管理系统(超详细版)
  • OpenProcessToken
  • 前端框架Layui学习五:弹出层和数据表格
  • 原神童梦的切片解密攻略
  • VMware虚拟机安装Linux教程(超详细) | 阿里龙蜥OS (CentOS替代品)