android 自定义view 属性参数值 高效获取

见注释部分和不注释部分,
注释部分,在for循环中,很效率低;


    private int tvwLenght;
    private String edtHint;
    private int tvwColor;
    private String tvwStr;


    TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.MyTvwEdt);
        tvwStr = array.getString(R.styleable.MyTvwEdt_mte_text);
        tvwColor = array.getColor(R.styleable.TwoTextopen_ttopen_textColor, Color.BLACK);
        tvwLenght=array.getInteger(R.styleable.MyTvwEdt_mte_lenght,20);
        edtHint = array.getString(R.styleable.MyTvwEdt_mte_hint);
        array.recycle();//属性获取完之后及时回收
//        int count = attrs.getAttributeCount();
//        for (int i = 0; i < count; i++) {
//            String attrName = attrs.getAttributeName(i);//获取属性名称
//            //根据属性获取资源ID
//            switch (attrName) {
//                //显示的文字
//                case "mte_text":
//                    textId = attrs.getAttributeResourceValue(i, 0);
//                    break;
//                //显示的文字的颜色
//                case "mte_textColor":
//                    textColorId = attrs.getAttributeResourceValue(i, 0);
//                    break;
//                //输入的字符长度
//                case "mte_lenght":
//                    tvwLenght = attrs.getAttributeResourceValue(i, 0);
//                    break;
//                case "mte_hint":
//                    hintId = attrs.getAttributeResourceValue(i, 0);
//                    break;
//
//            }
//        }

attr文件里的样例:

 <declare-styleable name="MyTvwEdt">
        <attr name="mte_text" format="reference" />
        <attr name="mte_textColor" format="reference|color" />
        <attr name="mte_lenght" format="integer"/>
        <attr name="mte_hint" format="reference"></attr>
    </declare-styleable>

xml布局中使用:
注意:在布局的顶部加“app”,你自己定义;写啥都行 ;
xmlns:app=”http://schemas.android.com/apk/res-auto”

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

 <!--机井编码-->
            <com.automic.app.view.MyTvwEdt
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:id="@+id/mte_wellstcd"
                app:mte_lenght="17"
                app:mte_hint="@string/wellstcdhint"
                app:mte_text="@string/wellStcd"
                app:mte_textColor="@color/black">

            </com.automic.app.view.MyTvwEdt>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不对法

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值