Android中TypedArray的用法

本文讲解了android页面对应的布局文件使用了自定义view,而自定义View添加了declare-styleable定义的自定义属性的基本的使用步骤。

1 首先,定义自定义属性,如下:

在res/values文件夹下定义一个attrs.xml文件,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="mytextview">
      <attr name="textColor" format="color" />
      <attr name="textSize" format="dimension"></attr>
   </declare-styleable>
</resources>

其中resource是根标签,可以在里面定义若干个declare-styleable<declare-styleable name="MyView">中name定义了变量的名称,其中<attr>中的name属性定义了属性名称,如textColor,format属性定义了属性的格式,在这里是color。下面可以再自定义多个属性,比如<attr name="myTextSize" format="dimension"/>来说,其属性的名称为"myTextSize",format指定了该属性类型为dimension,只能表示字体的大小。reference   表示引用,参考某一资源ID,另外还有其他类型如下:

string   表示字符串
color   表示颜色值
dimension   表示尺寸值
boolean   表示布尔值
integer   表示整型值
float   表示浮点值
fraction   表示百分数
enum   表示枚举值
flag   表示位运算


2 在自定义View中构造方法中获取属性,如下:

public class MyTextView extends TextView {
    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.mytextview);
        int textColor = ta.getColor(R.styleable.mytextview_textColor, 0XFF00FF00);
        float textSize = ta.getDimension(R.styleable.mytextview_textSize, 36);
        setTextColor(textColor);
        setTextSize(textSize);
        ta.recycle();
    }
}

在自定义的View中通过TypedArray a = context. obtainStyledAttributes (attrs,R.styleable.mytextview);

Int textColor =a.getColor(R.styleable.mytextview_textColor, 003344);

其中R.styleable.mytextview中mytextview是<declare-styleable>中 name的值。

通过TypeArray类提供了一系列方法,如getColor,getDimension方法获取属性值。

 int textColor = ta.getColor(R.styleable.mytextview_textColor, 0XFF00FF00);

其中mytextview_textColor是是<declare_styleable>中name的值+下划线+<attr>属性的值。


3 Activity页面的布局文件中使用此自定义类节点,设置自定义属性的数值,实例代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:test ="http://schemas.android.com/tools/com.example.user.testdemo"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
   
    <com.example.user.testdemo.MyTextView
       <span style="color:#ff0000;"> test:textSize="18sp"
        test:textColor="#00FF00"</span>
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我们"/>
</RelativeLayout>


需要在声明称将包引入工程xmlns:test ="http://schemas.android.com/tools/com.example.user.testdemo"

其中:test是自定义名称,可以随意起名,值是http://schemas.android.com/tools/+包名称

<MyTextView>添加自定义属性需要使用test:+属性名称,如test:textSize="18sp"




  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值