为 android的系统控件添加属性

 android 的系统控件添加属性

android  的开发过程如果控件需要使用自定义属性有两种方法:
一:继承原有控件,增加自定义的属性。
        这个方法可以参考以下文章:

二:修改控件的源码,对控件增加新的属性。本文主要讨论这种情况。
         比如对  TextView 控件添加  android:myattr  属性,这样就可以在  XML 直接用以下代码定义
        <TextView
        android:myattr= "true"
        android:text= "@string/hello_world" />

     第一步:在  framework  中新增属性,
修改  frameworks/base/core/res/res/values/attrs.xml
加入
   <attr name=”myattr” format=”boolean”>

     第二步:为这个属性增加  id
修改  frameworks/base/core/res/res/values/public.xml
加入
   <public type=”attr” name=”myattr” id=”0x010103cd”/>

注意这个  id  不要和已经有的  id 重复。  Id  中各个位的含义请看以下网页:
这里引用该文章的图:

Android  在编译过程中就是在生成的  out/target/common/R/android/R.java  中加入
public static final int myattr=0x010103cd;
public static final int TextView_myattr = 76;

     第三步:就可以在  TextView  类中使用  TextView_myattr
frameworks/base/core/java/android/widget/TextView.java
    private boolean myattr = false;
    public TextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
…
        a = theme.obtainStyledAttributes(
                    attrs, com.android.internal.R.styleable.TextView, defStyle, 0);

        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);

            switch (attr) {
…
            case com.android.internal.R.styleable.TextView_uureverse:
                myattr = a.getBoolean(attr, myattr);
                break;
…


剩下的就是在这个类中添加  myattr  的具体实现方法。

转载于:https://www.cnblogs.com/setsailwu/p/3852261.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值