这里为了演示使用自己定义变量,字体大小改用自己定义的属性。

首先要创建变量,创建了个values/attrs.xml文件,文件名称随意,可是要在values文件夹下:

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

<resources>   

    <declare-styleable name="button">   

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

    </declare-styleable>   

</resources>

根标签要是resources,定义的变量要有个名字,declare-styleable name="button">,这里定义名称为button。在这个名称里,能够有多个自己定义属性。定义了个名为textSize的属性,格式是dimension,这个format指定了textSize属性的类型,仅仅能用于定义字体大小。

在布局文件里通过自己定义属性赋值:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    xmlns:myapp="http://schemas.android.com/apk/res/com.easymorse.textbutton" 

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

    android:layout_height="fill_parent" android:background="@drawable/background_color"> 

    <LinearLayout android:layout_width="fill_parent" 

        android:layout_height="10dip" /> 

    <LinearLayout android:layout_width="fill_parent" 

        android:layout_height="40dip"> 

        <com.easymorse.textbutton.TextButton 

            android:layout_width="fill_parent" android:layout_height="fill_parent" 

            android:layout_weight="1" android:text="电影" 

            android:gravity="center_vertical|center_horizontal" 

            android:background="@drawable/button" android:focusable="true" 

            android:clickable="true" myapp:textSize="20sp" />

这里在根标签中添加了:

xmlns:myapp=http://schemas.android.com/apk/res/com.easymorse.textbutton

声明了myapp这个名字空间,myapp是随意的名称,自己能够随便起名,后面的:

http://schemas.android.com/apk/res/

是固定的。再后面接的是应用的包名。

在以下自己定义button中的:myapp:textSize,就是使用<attr name="textSize"这个变量了,给变量赋值。

还须要一个过程,就是在程序中获取到这个赋值:

public TextButton(final Context context, AttributeSet attrs) { 

    this(context, attrs, 0); 

    TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.button); 

    this.setTextSize(typedArray.getDimension(R.styleable.button_textSize, 15)); 

    typedArray.recycle();

当中,TypedArray实例是个属性的容器,context.obtainStyledAttributes()方法返回得到。AttributeSet是节点的属性集合,在本例中是<com.easymorse.textbutton.TextButton节点中的属性集合。

这句话:

typedArray.getDimension(R.styleable.button_textSize, 

                15)

将获取自己定义textSize的值,假设没有,则使用默认的值,15。

最后别忘记调用:

typedArray.recycle();

作用是:

Give back a previously retrieved StyledAttributes, for later re-use.

这里的自己定义属性的format,能够有非常多种:

  • reference
  • string
  • color
  • dimension
  • boolean
  • integer
  • float
  • fraction
  • enum
  • flag

android使用自己定义属性AttributeSet的更多相关文章

  1. Android自己定义控件而且使其能够在xml中自己定义属性

    为什么要自己定义View android开发中自己定义View的优点是显而易见的.比方说以下的这个顶部导航,它被设计出如今应用的每一个界面,但每次的内容却不尽同样.我们不能在每一个layout资源中都 ...

  2. Android中自己定义组件和它的属性

    好长时间没有更新博客了.本来想积累点有深度的东西发,但一直没有找到非常好的点.所以.写一些基础的东西.就当积累吧. Android开发中难免会用到自己定义的组件.以下以ImageButton为例来介绍 ...

  3. Android 它们的定义View (一)

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/24252901 非常Android入门程序员AndroidView.可能都是比較恐 ...

  4. Android 它们的定义View它BounceProgressBar

    转载请注明出处:http://blog.csdn.net/bbld_/article/details/41246247 [Rocko's blog] 之前几天下载了非常久没用了的桌面版酷狗来用用的时候 ...

  5. android:自己定义组合控件Weight(高仿猫眼底部菜单条)

    在我们实际开发其中.会碰见一些布局结构类似或者同样的界面.比如应用的设置界面.tabbutton界面等. 这时候.对于刚開始学习的人来说,xml里面一个个绘制出来也许是最初的想法.可能随着经验的积累, ...

  6. android自定义控件(1)-自定义控件属性

    那么还是针对我们之前写的自定义控件:开关按钮为例来说,在之前的基础上,我们来看看有哪些属性是可以自定义的:按钮的背景图片,按钮的滑块图片,和按钮的状态(是开还是关),实际上都应该是可以在xml文件中直 ...

  7. 【Android】自己定义View

    翻译自:http://developer.android.com/training/custom-views/index.html 一)创建view类 一个设计良好的自己定义view与其它的类一样.它 ...

  8. android中xml tools属性详解

    第一部分 安卓开发中,在写布局代码的时候,ide可以看到布局的预览效果. 但是有些效果则必须在运行之后才能看见,比如这种情况:TextView在xml中没有设置任何字符,而是在activity中设置了 ...

  9. Android读取自定义View属性

    Android读取自定义View属性 attrs.xml : <?xml version="1.0" encoding="utf-8"?> < ...

随机推荐

  1. live555学习经验链接二

    live555学习经验链接二:http://blog.csdn.net/nkmnkm/article/category/1066093/2

  2. LayoutInflater作用是将layout的xml布局文件实例化为View类对象。

    获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.L ...

  3. 关于SpringMVC中找不到<mvc:resources/>标签的解决办法

    在springMVC中我们经常会用到<mvc:resources/>标签,但是有些编辑器中的schema过于陈旧.导致找不到<mvc:resources/>标签. 经过试验,有 ...

  4. 简单的REST的框架实现

    源代码下载地址:http://download.csdn.net/source/1662193 一. 认识REST REST软件架构是由Roy Thomas Fielding博士在2000年首次提出的 ...

  5. javadoc入门

    斌斌 (给我写信) 原创博文(http://blog.csdn.net/binbinxyz),转载请注明出处! java凝视 java里面有两种类型的凝视.一种是以"/*"起头,以 ...

  6. 转载 js判断浏览器

    $scope.identifyBrowser= function () { var userAgent = navigator.userAgent, rMsie = /(msie\s|trident. ...

  7. SASS安装的那些事

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  8. objective-C学习笔记(二)类 class 和 结构 struct

    Objective-C的类型 引用类型 类 class 指针 pointer 块 block 值类型 基础数值类型 结构 struct 枚举 enum 类型装饰 协议 protocol 类别 cate ...

  9. linux kernel中timer的使用

    linux kernel中timer的使用 http://blog.csdn.net/njuitjf/article/details/16888821 在kernel中如果想周期性的干些什么事情,或者 ...

  10. 数据层使用DBHelper.dll来减少工作量

    目前在需求确定了以后进行开发的步骤一般是这样的:建立数据表,建立model,构建数据操作层,最后在页面中进行调用.关于数据操作层,因为大量的操作都集中在增加.更新.删除等简易操作,而我之前的写法是每次 ...