http://blog.csdn.net/lihenair/article/details/41009711

工作中时常需要自定义控件,除了按键,draw以外,还需要对控件属性进行一些初始化的操作,比如控件的边距,字体大小,颜色等。

本文将根据需求,实现一个自定义的TextView。

1 需求

要求TextView的字体是30sp,颜色是#FF00FFAD。

针对这两个需求,做出如下定义

colors.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <color name="textcolor">#FF00FFAD</color>
  4. </resources>

dimens.xml

  1. <resources>
  2. <!-- Default screen margins, per the Android Design guidelines. -->
  3. <dimen name="activity_horizontal_margin">16dp</dimen>
  4. <dimen name="activity_vertical_margin">16dp</dimen>
  5. <dimen name="text_size">30sp</dimen>
  6. </resources>

2 定义属性声明

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="LargeText">
  4. <attr name="textsize" format="dimension" />
  5. <attr name="textcolor" format="color" />
  6. </declare-styleable>
  7. </resources>

这里需要注意的是format属性的定义,format="reference"一般用于字符串,表示引用的是某个string的定义

3 导入控件

有了以上定义,我们就可以在layout文件中加入以上定义的属性了

main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. xmlns:LargeText="http://schemas.android.com/apk/res/com.example.nfctest" //引入自定义属性的风格
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:paddingBottom="@dimen/activity_vertical_margin"
  7. android:paddingLeft="@dimen/activity_horizontal_margin"
  8. android:paddingRight="@dimen/activity_horizontal_margin"
  9. android:paddingTop="@dimen/activity_vertical_margin"
  10. tools:context=".NFCActivity" >
  11. <com.example.nfctest.LargeText android:id="@+id/state"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="@string/hello_world"
  15. LargeText:textsize="@dimen/text_size"     //引用自定义的字体大小
  16. LargeText:textcolor="@color/textcolor" /> //引用自定义的颜色
  17. </RelativeLayout>

引入自定义控件在layout中需要包含packagename,格式是<package-name>.<customize-class_name>

自定义属性风格需要在layout或者view的属性列加载,格式是xmlns:<style-name>=“http://schemas.Android.com/apk/res/<package-name>”

使用自定义属性的格式是<style-name>:<attrs-name>

4 万事俱备,现在需要定义LargeText.java

LargeText.java

  1. package com.example.nfctest;
  2. import android.content.Context;
  3. import android.content.res.TypedArray;
  4. import android.util.AttributeSet;
  5. import android.widget.TextView;
  6. public class LargeText extends TextView {
  7. public LargeText(Context context) {
  8. this(context, null, 0);
  9. // TODO Auto-generated constructor stub
  10. }
  11. public LargeText(Context context, AttributeSet attrs) {
  12. this(context, attrs, 0);
  13. // TODO Auto-generated constructor stub
  14. }
  15. public LargeText(Context context, AttributeSet attrs, int defStyle) {
  16. super(context, attrs, defStyle);
  17. // TODO Auto-generated constructor stub
  18. TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LargeText, defStyle, 0);
  19. int textColor = a.getColor(R.styleable.LargeText_textcolor,
  20. 0XFFFFFFFF);
  21. float textSize = a.getDimension(R.styleable.LargeText_textsize, 36);
  22. a.recycle(); //使用类型数组后,需要回收它
  23. setTextSize(textSize);
  24. setTextColor(textColor);
  25. }
  26. }

通过以上4步,自定义的textview就会按照我们定义的属性去显示文字了。

Android中如何使用xmlns的更多相关文章

  1. 如何理解Android中的xmlns

    转发自:https://www.jianshu.com/p/6fcaffaeffd2 作为一名 Android 开发,我想大家对xmlns并不会陌生,因为在写布局文件(如下代码所示)的时候经常会碰到, ...

  2. Android中的LinearLayout布局

    LinearLayout : 线性布局 在一般情况下,当有很多控件需要在一个界面列出来时,我们就可以使用线性布局(LinearLayout)了,  线性布局是按照垂直方向(vertical)或水平方向 ...

  3. Android中BroadcastReceiver的两种注册方式(静态和动态)详解

    今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...

  4. Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)

    之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...

  5. Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)

    昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...

  6. Android中Fragment和ViewPager那点事儿(仿微信APP)

    在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...

  7. Android中的flexboxlayout布局

    提到FlexboxLayout大家估计有点模糊,它是谷歌最近开源的一个android排版库,它的前身Flexbox是2009年W3C提出了一种新的布局,可以简便.完整.响应式的实现页面布局,Flexb ...

  8. Android中使用ExpandableListView实现好友分组

    一个视图显示垂直滚动两级列表中的条目.这不同于列表视图,允许两个层次,类似于QQ的好友分组.要实现这个效果的整体思路为: 1.要给ExpandableListView 设置适配器,那么必须先设置数据源 ...

  9. Android中使用ViewPager实现屏幕页面切换和页面切换效果

    之前关于如何实现屏幕页面切换,写过一篇博文<Android中使用ViewFlipper实现屏幕切换>,相比ViewFlipper,ViewPager更适用复杂的视图切换,而且Viewpag ...

随机推荐

  1. Jenkins持续集成学习-Windows环境进行.Net开发4

    目录 Jenkins持续集成学习-Windows环境进行.Net开发4 目录 前言 目标 Github持续集成 提交代码到Github 从Github更新代码 git上显示构建状态 自动触发构建 Gi ...

  2. PHP报错类型

    p: 语法错误 定界符; F: 致命错误 函数;方法重名;包含(require) W: 警告 包含(include); N: 通知 变量未声明,函数

  3. vmware--查看链接克隆依赖关系

    我们都知道,虚拟机克隆有完全克隆和链接克隆两种克隆方式.当根据模版去链接克隆出很多机器时,时间一长或者把克隆后的机器改名了,我们就忘记了哪台机器是克隆出来的,哪台是直接装的.如果不小心把模版机器给删除 ...

  4. Apollo 2 如何支持 @Value 注解自动更新

    前言 Apollo 在 v0.10.0 版本后,支持自动更新.v0.10.0之前的版本在配置变化后不会重新注入,需要重启才会更新. 也就是说,如果一个属性加入了 @Value 注解,并且这个配置在配置 ...

  5. JavaScript之破解数独(附详细代码)

      在上一篇分享中,我们用Python和Django来破解数独,这对不熟悉Python和Django的人来说是非常不友好的.这次,笔者只用HTML和JavaScript写了破解数独的程序,对于熟悉前端 ...

  6. 数据库内连接GROUP BY查询外键表数据行的总数

    最近看了看SQL,刚好遇到这个问题. INNER JOIN [外键表] ON [主键表] 内链接,用 GROUP BY 分组外键数据,COUNT(*)计算该外键数据总行数,最后用 ORDER BY 排 ...

  7. c# 封装Dapper操作类

    using Dapper; using DapperExtensions; using System.Collections.Generic; using System.Configuration; ...

  8. hadoop fs,hadoop dfs,hdfs dfs

    hadoop fs: FS relates to a generic file system which can point to any file systems like local, HDFS ...

  9. c++中的this指针和c#中的this引用

    先总结一下: 在c++中this为指针,使用"->"操作符来获取当前实例中的成员 在c#中this为引用,使用"."操作符来获取当前实例中的成员 下面内容 ...

  10. BZOJ2820: YY的GCD(反演)

    题解 题意 题目链接 Sol 反演套路题.. 不多说了,就是先枚举一个质数,再枚举一个约数然后反演一下. 最后可以化成这样子 \[\sum_{i = 1}^n \frac{n}{k} \frac{n} ...