【转】自定义Android控件属性

Android原有的属性可能不能满足我们现在要做的事,毕竟有些人就是会天马行空的想出一些Android不会做的东西。今天就简单的写下怎样为自定义控件自定义属性,看这种描述有点晕,转过来就是控件和属性都是自定义的吧。哈~

上面是运行界面,有两个自定义的Button,主要是用来区分。

首先在res/values/目录下新建attrs.xml文件,用来自定义属性

复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyCustomWidget">
        <attr name="myText" format="string"/>
        <attr name="isEnable" format="boolean"/>
    </declare-styleable>
</resources>
复制代码

标签declare-styleable的name值为可以通过这个值获取相关属性,这表明把这些属性赋给自定义的控件。

下面在MyCustomWidget.java的构造函数总从xml属性中读取到设置的值,利用获得的值就可以做自己的事了,代码如下:

复制代码
package nbe.sense7.vinci.custom.xmlattrs;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.Button;

public class CustomWedgit extends Button {

    public CustomWedgit(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.MyCustomWidget);

        final int indexCount = a.getIndexCount();
        for (int i = 0; i < indexCount; ++i) {
            int attr = a.getIndex(i);
            switch (attr) {
            case R.styleable.MyCustomWidget_myText:
                //获取myText属性值
                String myText = a.getString(attr);
                //得到值后就可以按你想要的使用了,我这里是给Button设置按钮显示文字
                CustomWedgit.this.setText(myText);
                break;
            case R.styleable.MyCustomWidget_isEnable:
                boolean isEnable = a.getBoolean(attr, false);
                // 这里利用布尔属性值做自己想做的
                break;
            }
        }
        a.recycle();
    }

}
复制代码

最后把自定义的属性加入到layout里面,main.xml:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:vinci="http://schemas.android.com/apk/res/nbe.sense7.vinci.custom.xmlattrs"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <nbe.sense7.vinci.custom.xmlattrs.CustomWedgit
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button自带text属性设置的值" />
    
    <nbe.sense7.vinci.custom.xmlattrs.CustomWedgit
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        vinci:myText="Button自定义myText属性设置的值" />

</LinearLayout>
复制代码

最重要的一点是别忘了要加入上面深色的一行,xmlns:后面可以加入自定义的名字,我的是vinici,对于引号里面的值应该是xmlns:vinci="http://schemas.android.com/apk/res/你的包名"。

OK~写完手工!!

 
分类: Android

转载于:https://www.cnblogs.com/DonkeyTomy/articles/2548317.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值