Android中如何使用xmlns

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

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

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

1 需求

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

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

colors.xml

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <color name="textcolor">#FF00FFAD</color>  
  4. </resources>  

dimens.xml

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <resources>  
  2.   
  3.     <!-- Default screen margins, per the Android Design guidelines. -->  
  4.     <dimen name="activity_horizontal_margin">16dp</dimen>  
  5.     <dimen name="activity_vertical_margin">16dp</dimen>  
  6.       
  7.     <dimen name="text_size">30sp</dimen>  
  8.   
  9. </resources>  


2 定义属性声明

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  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

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  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.   
  12.     <com.example.nfctest.LargeText android:id="@+id/state"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="@string/hello_world"  
  16.         LargeText:textsize="@dimen/text_size"     //引用自定义的字体大小  
  17.         LargeText:textcolor="@color/textcolor" /> //引用自定义的颜色  
  18.   
  19. </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

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.example.nfctest;  
  2.   
  3. import android.content.Context;  
  4. import android.content.res.TypedArray;  
  5. import android.util.AttributeSet;  
  6. import android.widget.TextView;  
  7.   
  8. public class LargeText extends TextView {  
  9.   
  10.     public LargeText(Context context) {  
  11.         this(context, null, 0);  
  12.         // TODO Auto-generated constructor stub  
  13.     }  
  14.       
  15.     public LargeText(Context context, AttributeSet attrs) {  
  16.         this(context, attrs, 0);  
  17.         // TODO Auto-generated constructor stub  
  18.     }  
  19.       
  20.     public LargeText(Context context, AttributeSet attrs, int defStyle) {  
  21.         super(context, attrs, defStyle);  
  22.         // TODO Auto-generated constructor stub  
  23.         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LargeText, defStyle, 0);  
  24.         int textColor = a.getColor(R.styleable.LargeText_textcolor,    
  25.                 0XFFFFFFFF);    
  26.         float textSize = a.getDimension(R.styleable.LargeText_textsize, 36);  
  27.         a.recycle(); //使用类型数组后,需要回收它  
  28.         setTextSize(textSize);  
  29.         setTextColor(textColor);  
  30.     }  
  31.   
  32. }  


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



原文地址:https://www.cnblogs.com/feng9exe/p/5657852.html