自定义属性(xml中可插入)

前端之家收集整理的这篇文章主要介绍了自定义属性(xml中可插入)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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

文件夹中创建attrs.xml

如同:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. - <resources>
  3. - <declare-styleable name="MyTabExchangeView">
  4. <attr name="textColor" format="color" />
  5. <attr name="textSize" format="dimension" />
  6. <attr name="textString" format="string" />
  7. <attr name="iconColor" format="color" />
  8. <attr name="iconDrawable" format="reference|color" />
  9. </declare-styleable>
  10. </resources>

formate属性值说明:

1. reference:资源id

2. color:颜色值

3. boolean:布尔值

4. dimension:尺寸值

5. float:浮点值

6. integer:整型值

7. string:字符串

8. fraction:百分数

9. enum:枚举值

10. flag:位或运算


自定义属性使用:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. xmlns:ww="http://schemas.android.com/apk/res/com.ww.mybottomtitle"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. >
  7.  
  8. <android.support.v4.view.ViewPager
  9. android:id="@+id/fragment_pager"
  10. android:layout_width="fill_parent"
  11. android:layout_height="fill_parent"
  12. android:layout_above="@+id/bottom_title"
  13. />
  14. <LinearLayout
  15. android:id="@+id/bottom_title"
  16. android:layout_width="fill_parent"
  17. android:layout_height="48dp"
  18. android:layout_alignParentBottom="true"
  19. android:orientation="horizontal"
  20. android:background="#F0F0F0"
  21. >
  22. <com.ww.mybottomtitle.MyTabExchangeView
  23. android:id="@+id/index"
  24. android:layout_width="0dp"
  25. android:layout_height="fill_parent"
  26. android:layout_weight="1"
  27. ww:textString="首页"
  28. android:paddingTop="5dp"
  29. android:paddingBottom="5dp"
  30. ww:iconDrawable="@drawable/icon_home"
  31. ww:textSize="10sp"
  32. />
  33. <com.ww.mybottomtitle.MyTabExchangeView
  34. android:id="@+id/news"
  35. android:layout_width="0dp"
  36. android:layout_height="fill_parent"
  37. android:layout_weight="1"
  38. android:paddingTop="5dp"
  39. android:paddingBottom="5dp"
  40. ww:textString="咨询"
  41. ww:iconDrawable="@drawable/icon_dingyue"
  42. ww:textSize="10sp"
  43. />
  44. <com.ww.mybottomtitle.MyTabExchangeView
  45. android:id="@+id/happy"
  46. android:layout_width="0dp"
  47. android:layout_height="fill_parent"
  48. android:layout_weight="1"
  49. android:paddingTop="5dp"
  50. android:paddingBottom="5dp"
  51. ww:textString="娱乐"
  52. ww:iconDrawable="@drawable/icon_list"
  53. ww:textSize="10sp"
  54. />
  55. <com.ww.mybottomtitle.MyTabExchangeView
  56. android:id="@+id/myself"
  57. android:layout_width="0dp"
  58. android:layout_height="fill_parent"
  59. android:layout_weight="1"
  60. android:paddingTop="5dp"
  61. android:paddingBottom="5dp"
  62. ww:textString="我"
  63. ww:iconDrawable="@drawable/icon_health_data"
  64. ww:textSize="10sp"
  65. />
  66. </LinearLayout>
  67. </RelativeLayout><strong>
  68. </strong>


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

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

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

如下:

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

猜你在找的XML相关文章