自定义属性xml中可插入

在学习开发过程中有许多时候需要使用自定义控件。。然而如何在布局过程中更加方便的定义自定义的属性呢?

就拿上一篇博客:仿微信6.0底部菜单选择和滑动效果中自定义底部菜单中的控件来说。。在控件中

我们需要规定渐变颜色和字体大小等属性,为了像android:textsize="14sp"这样方便的开发。。我们可以在项目文件夹下的res\values

文件夹中创建attrs.xml

如同:

<?xml version="1.0" encoding="utf-8" ?> 
- <resources>
- <declare-styleable name="MyTabExchangeView">
  <attr name="textColor" format="color" /> 
  <attr name="textSize" format="dimension" /> 
  <attr name="textString" format="string" /> 
  <attr name="iconColor" format="color" /> 
  <attr name="iconDrawable" format="reference|color" /> 
  </declare-styleable>
  </resources>

formate属性值说明:

1. reference:资源id

2. color:颜色值

3. boolean:布尔值

4. dimension:尺寸值

5. float:浮点值

6. integer:整型值

7. string:字符串

8. fraction:百分数

9. enum:枚举值

10. flag:位或运算


自定义属性使用:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ww="http://schemas.android.com/apk/res/com.ww.mybottomtitle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <android.support.v4.view.ViewPager
        android:id="@+id/fragment_pager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/bottom_title"
    />
    <LinearLayout
        android:id="@+id/bottom_title" 
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:background="#F0F0F0"
        >
        <com.ww.mybottomtitle.MyTabExchangeView
            android:id="@+id/index"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            ww:textString="首页"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            ww:iconDrawable="@drawable/icon_home"
            ww:textSize="10sp"
            />
        <com.ww.mybottomtitle.MyTabExchangeView
            android:id="@+id/news"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            ww:textString="咨询"
            ww:iconDrawable="@drawable/icon_dingyue"
            ww:textSize="10sp"
            />
        <com.ww.mybottomtitle.MyTabExchangeView
            android:id="@+id/happy"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            ww:textString="娱乐"
            ww:iconDrawable="@drawable/icon_list"
            ww:textSize="10sp"
            />
        <com.ww.mybottomtitle.MyTabExchangeView
            android:id="@+id/myself"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            ww:textString="我"
            ww:iconDrawable="@drawable/icon_health_data"
            ww:textSize="10sp"
            />
            
    </LinearLayout>
</RelativeLayout><strong>
</strong>


要注意的是我们首先要引入我们的包名,,告诉编译器该属性在我们自己的项目中查找而不是在android定义的包中寻找。

xmlns:ww="http://schemas.android.com/apk/res/com.ww.mybottomtitle"其中ww是可以自己定义,但是后面的包名com.ww.mybottomtitle必须是整个项目的包名,我在最开始使用的时候就是犯了这个错误误以为是自定义控件所在的包名,最终错误。。。。你们懂的。。。debug调试调的差点崩溃。。

此处定义属性之后如何在我们定义的控件中获取到该数据呢?

如下:

 TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.MyTabExchangeView);  
        for (int i = 0; i < array.getIndexCount(); i++) {  
            int type=array.getIndex(i);  
            switch (type) {  
            case R.styleable.MyTabExchangeView_iconColor:  
                iconColor=array.getColor(type,0xff45c01a);  
                break;  
            case R.styleable.MyTabExchangeView_iconDrawable:  
                Drawable iconBitmapDrawable = array.getDrawable(type);  
                iconBitmap=((BitmapDrawable)iconBitmapDrawable).getBitmap();  
                break;  
            case R.styleable.MyTabExchangeView_textColor:  
                textColor=array.getColor(type,0xff45c01a);  
                break;  
            case R.styleable.MyTabExchangeView_textSize:  
                textSize=array.getDimension(type,10);  
                break;  
            case R.styleable.MyTabExchangeView_textString:  
                textString =array.getString(type);  
                break;  
            default:  
                break;  
            }  
        }  
        array.recycle();  
这样我们就可以得到我们从xml中所写入的数据啦。。是不是很方便呢?当然为了保持以后使用该属性的一致性我们需要进行array.recycle() 了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇