在Android开发中,往往要用到自定义的控件来实现我们的需求或效果。在使用自定义 
控件时,难免要用到自定义属性,那怎么使用自定义属性呢?

一、简单使用:

1.在文件res/values/下新建attrs.xml属性文件,中定义我们所需要的属性:
<resources><!-- resource是跟标签,可以在里面定义若干个declare-styleable -->
<declare-styleable name="custom_view"><!-- name定义了变量的名称 --><attr name="custom_color" format="color"></attr> <!-- 定义对应的属性,name定义了属性的名称 --><attr name="custom_size" format="dimension"></attr> <!--每一个发生要定义format指定其类型,类型包括 reference   表示引用,参考某一资源IDstring   表示字符串color   表示颜色值dimension   表示尺寸值boolean   表示布尔值integer   表示整型值float   表示浮点值fraction   表示百分数enum   表示枚举值flag   表示位运算-->
</declare-styleable>
 

2.在布局中使用:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:ldm="http://schemas.android.com/apk/res/com.ldm.learn"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#f6f6f6"android:orientation="vertical"android:padding="10dp" ><com.ldm.learn.CustomTextViewandroid:layout_width="100dp"android:layout_height="100dp"android:text="自定义TextView"ldm:custom_color="#333333"ldm:custom_size="35sp" /></LinearLayout>
2.在代码中引用:
public class CustomTextView extends TextView {
 private int textSize;//自定义文件大小

private int textColor;//自定义文字颜色

//自定义属性,会调用带两个参数的构造方法
    public CustomTextView(Context context, AttributeSet attrs) {
           super(context, attrs);
           TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.custom_view);//TypedArray属性对象
            textSize = ta.getDimensionPixelSize(R.styleable.custom_view_custom_size, 20);//获取属性对象中对应的属性值
           textColor = ta.getColor(R.styleable.custom_view_custom_color, 0x0000ff);
           setColorAndSize(textColor, textSize);//设置属性
           ta.recycle();
}

public CustomTextView(Context context){

super(context);

}

private void setColorAndSize(int textColor, int textSize){

setTextColor(textColor);

setTextSize(textSize);

}

 

}

二、declare-styleable属性详解:

1. reference:参考某一资源ID。

(1)属性定义:

<declare-styleable name = "名称">

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

</declare-styleable>

(2)属性使用:

<ImageView

android:layout_width = "42dip"
                     android:layout_height = "42dip"
                     android:background = "@drawable/图片ID"

/>

2. color:颜色值。

(1)属性定义:

<declare-styleable name = "名称">

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

</declare-styleable>

(2)属性使用:

<TextView

android:layout_width = "42dip"
                     android:layout_height = "42dip"
                     android:textColor = "#00FF00"

/>

3. boolean:布尔值。

(1)属性定义:

<declare-styleable name = "名称">

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

</declare-styleable>

(2)属性使用:

<Button

android:layout_width = "42dip"
                    android:layout_height = "42dip"

android:focusable = "true"

/>

4. dimension:尺寸值。

(1)属性定义:

<declare-styleable name = "名称">

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

</declare-styleable>

(2)属性使用:

<Button

android:layout_width = "42dip"
                    android:layout_height = "42dip"

/>

5. float:浮点值。

(1)属性定义:

<declare-styleable name = "AlphaAnimation">

<attr name = "fromAlpha" format = "float" />
                   <attr name = "toAlpha" format = "float" />

</declare-styleable>

(2)属性使用:

<alpha
                   android:fromAlpha = "1.0"
                   android:toAlpha = "0.7"

/>

6. integer:整型值。

(1)属性定义:

<declare-styleable name = "AnimatedRotateDrawable">

<attr name = "visible" />
                   <attr name = "frameDuration" format="integer" />
                   <attr name = "framesCount" format="integer" />
                   <attr name = "pivotX" />
                   <attr name = "pivotY" />
                   <attr name = "drawable" />

</declare-styleable>

(2)属性使用:

<animated-rotate

xmlns:android = "http://schemas.android.com/apk/res/android" 
                   android:drawable = "@drawable/图片ID" 
                   android:pivotX = "50%" 
                   android:pivotY = "50%" 
                   android:framesCount = "12" 
                   android:frameDuration = "100"

/>

7. string:字符串。

(1)属性定义:

<declare-styleable name = "MapView">
                   <attr name = "apiKey" format = "string" />
            </declare-styleable>

(2)属性使用:

<com.google.android.maps.MapView
                    android:layout_width = "fill_parent"
                    android:layout_height = "fill_parent"
                    android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"

/>

8. fraction:百分数。

(1)属性定义:

<declare-styleable name="RotateDrawable">
                   <attr name = "visible" />
                   <attr name = "fromDegrees" format = "float" />
                   <attr name = "toDegrees" format = "float" />
                   <attr name = "pivotX" format = "fraction" />
                   <attr name = "pivotY" format = "fraction" />
                   <attr name = "drawable" />
            </declare-styleable>

(2)属性使用:

<rotate

xmlns:android = "http://schemas.android.com/apk/res/android"
               android:interpolator = "@anim/动画ID"

android:fromDegrees = "0"
               android:toDegrees = "360"

android:pivotX = "200%"

android:pivotY = "300%"
               android:duration = "5000"

android:repeatMode = "restart"

android:repeatCount = "infinite"

/>

9. enum:枚举值。

(1)属性定义:

<declare-styleable name="名称">
                   <attr name="orientation">
                          <enum name="horizontal" value="0" />
                          <enum name="vertical" value="1" />
                   </attr>

</declare-styleable>

(2)属性使用:

<LinearLayout

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

10. flag:位或运算。

(1)属性定义:

<declare-styleable name="名称">
                    <attr name="windowSoftInputMode">
                            <flag name = "stateUnspecified" value = "0" />
                            <flag name = "stateUnchanged" value = "1" />
                            <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" />
                            <flag name = "adjustResize" value = "0x10" />
                            <flag name = "adjustPan" value = "0x20" />
                            <flag name = "adjustNothing" value = "0x30" />
                     </attr>

</declare-styleable>

(2)属性使用:

<activity

android:name = ".StyleAndThemeActivity"
                   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>

特别要注意:

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

(1)属性定义:

<declare-styleable name = "名称">

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

</declare-styleable>

(2)属性使用:

<ImageView

android:layout_width = "42dip"
                     android:layout_height = "42dip"
                     android:background = "@drawable/图片ID|#00FF00"

/>

转载:http://blog.csdn.net/langxinlen/article/details/50343175

转载于:https://www.cnblogs.com/zly1022/p/7526198.html

谈谈- declare-styleable属性相关推荐

  1. android 自定义 styleable 属性

    1.定义 attr.xml中定义  <resources><declare-styleable name="ViewFlow"><attr name= ...

  2. 详解CSS position属性

    原文地址:http://luopq.com/2015/11/15/css-position/ position是CSS中非常重要的一个属性,通过position属性,我们可以让元素相对于其正常位置,父 ...

  3. 利用Gallery和ImageView实现图片浏览器

    首先,先看一下实现的截图: 如图所示, 其中布局的第一个控件是Gallery,显示的图片滑动浏览,这里用到一个继承自BaseAdapter的类对象,用于填充和显示Gallery中的内容: 布局的第二个 ...

  4. xamarin.android 控件,Android 库控件 - Xamarin | Microsoft Docs

    Xamarin Android 库控件Xamarin.Android Gallery control 03/15/2018 本文内容 Gallery是一种布局小组件,用于显示水平滚动列表中的项,并将当 ...

  5. Zookeeper基础常用操作以及ACL权限

    这次将Zookeeper的一些基础用法以及权限这块的都补充一下在这篇博客中. 上篇博客介绍了基于ZooKeeper实现的分布式锁,也介绍了一些ZooKeeper的节点类型以及监听机制,今天这里就不作过 ...

  6. Android Gallery组件实现循环显示图像

    Gallery组件主要用于横向显示图像列表,只是按常规做法.Gallery组件仅仅能有限地显示指定的图像.也就是说,假设为Gallery组件指定了10张图像,那么当Gallery组件显示到第10张时, ...

  7. (转) android UI进阶之用gallery实现可滑动的Tab

    首先我们需要写Gallery的适配器.这里我们要注意的是Gallery有一个特点,就是起始一个元素的左边会留下一块空位,如下图所示: 这样我们的Tab显然不是很完美,如何解决?开始想的就是去看gall ...

  8. 设置版权声明html,在Hexo中自动为Yilia主题增加版权声明

    起因 写独立个人博客在与别人分享自己的经验和心情的同时,也要保护自己的权益.在文章的结尾为文章添加版权申明,明确的告知了他人可以获得怎样的许可,有朝一日也为自己以后维护权益提供了便利. 本文介绍在Yi ...

  9. android Gallery实现异步加载网络图片

    之前在网上找了很多都没有这方面的资料,大概的效果是当Gallery滑动时不下载图片,当Gallery滑动停止时加载当前页面图片,自己花了一点时间大概的实现了,如果各位有更好的意见欢迎说出来大家一起学习 ...

  10. Android中ImageSwitcher结合Gallery展示SD卡中的资源图片

    本文主要是写关于ImageSwitcher结合Gallery组件如何展示SDCard中的资源图片,相信大家都看过API Demo 中也有关于这个例子的,但API Demo 中的例子是展示工程中Draw ...

最新文章

  1. [异常解决] ubuntukylin16.04 LTS中关于flash安装和使用不了的问题解决
  2. linux下编译wpa_supplicantnbsp;…
  3. python self理解_Python面向对象总结,让你快速掌握面向对象常用的用法
  4. centos6 升级openssh7.0
  5. k8s、jenkins集成
  6. dlib疲劳检测_使用OpenCv和Dlib进行打哈欠检测
  7. mac os x 安装mysql遇到 Access denied for user 'root'@'localhost' (using password: YES)的解决方法...
  8. :将照片处理成绘画风格
  9. 《数学建模算法与应用》第2版 司守奎 孙兆亮及其习题解答两本书的配套程序及数据
  10. linux 网络编程与 windows 网络编程
  11. 谜一样的科学家——阿兰图灵
  12. git | 如何移除 Untracked files?
  13. 2021年江苏镇江公务员考试报考指南
  14. Python3简单爬虫:爬取猫眼评分top100电影
  15. 亚马逊出单技巧 掌握财富密码
  16. matlab 画渐近线,如何绘制渐近线?
  17. 从零开始学习VIO笔记 --- 第一讲:基础知识(四元数,李代数)
  18. Linux 配置GitHub秘钥 并上传
  19. java毕业设计校园互助平台校园帮帮网站源码+lw文档+mybatis+系统+mysql数据库+调试
  20. Tensorflow object detection API 的环境配置

热门文章

  1. listView 多布局
  2. Android用户界面程序设计示例
  3. 关于base64编码的原理及实现
  4. 深入浅出Nintex——更新PeopleandGroup类型的Field
  5. 智能家居 (8) ——智能家居项目整合(网络控制线程、语音控制线程,火灾报警线程)
  6. linux进度条脚本,Linux下简易进度条的实现代码
  7. xcode常用快捷键_Mac及Xcode常用快捷键
  8. 临汾移动搜索引擎推广_竞价信息流移动搜索推广分析!
  9. 操作系统:提升电脑开机速度的15个小技巧
  10. 你知道为啥程序员下班后只关显示器从不关电脑?