给自定义View添加xml属性

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

笔者之前已经写过了一些自定义View的文章,在此对其也就不从头说起了,如有兴趣的读者可以看一下笔者的前两篇文章
android 自定义view的使用(最佳demo——返回标题栏)
android 自定义控件(底部icon点击效果)

笔者之前的文章中仅仅介绍了如何使用自定义View以及为什么要使用自定义View等等,但是在实际操作中,我们还是希望自定义View之后,直接能够在xml中就对其进行操作,如下图:

那么如何操作呢?主要是三个步骤:

1、自定义属性名称

2、将属性名称与控件关联

3、从第三方命名空间获取自定义属性名称

主要代码

1、自定义属性名称

首先要在values文件中创建一个xml文件,并且在其中写上你需要的自定义属性名称以及类型。

atts.xml中代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="MyTitle">
  4. <attr name="textColor" format="color"/>
  5. <attr name="titleText" format="string"/>
  6. <attr name="leftText" format="string"/>
  7. <attr name="rightText" format="string"/>
  8. </declare-styleable>
  9. </resources>

2、将属性名称与控件关联

此点比较简单,直接看代码
MyView.java

  1. package com.example.double2.viewxmltest;
  2.  
  3. import android.content.Context;
  4. import android.content.res.TypedArray;
  5. import android.graphics.Color;
  6. import android.util.AttributeSet;
  7. import android.view.LayoutInflater;
  8. import android.widget.LinearLayout;
  9. import android.widget.TextView;
  10.  
  11. /** * 项目名称:ViewXmlTest * 创建人:Double2号 * 创建时间:2016/8/4 10:23 * 修改备注: */
  12. public class MyView extends LinearLayout {
  13.  
  14. private int colorText;
  15. private String textLeft;
  16. private String textTitle;
  17. private String textRight;
  18. private TextView tvLeft;
  19. private TextView tvTitle;
  20. private TextView tvRight;
  21.  
  22. public MyView(Context context,AttributeSet attrs,int defStyleAttr) {
  23. super(context,attrs,defStyleAttr);
  24.  
  25. //从xml的属性获取到字体颜色与string
  26. TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.MyTitle);
  27. colorText=ta.getColor(R.styleable.MyTitle_textColor,Color.BLACK);
  28. textLeft=ta.getString(R.styleable.MyTitle_leftText);
  29. textTitle=ta.getString(R.styleable.MyTitle_titleText);
  30. textRight=ta.getString(R.styleable.MyTitle_rightText);
  31. ta.recycle();
  32.  
  33. //获取到控件
  34. //加载布局文件,与setContentView()效果一样
  35. LayoutInflater.from(context).inflate(R.layout.my_view,this);
  36. tvLeft=(TextView)findViewById(R.id.tv_left);
  37. tvTitle=(TextView)findViewById(R.id.tv_title);
  38. tvRight=(TextView)findViewById(R.id.tv_right);
  39.  
  40. //将控件与设置的xml属性关联
  41. tvLeft.setTextColor(colorText);
  42. tvLeft.setText(textLeft);
  43. tvTitle.setTextColor(colorText);
  44. tvTitle.setText(textTitle);
  45. tvRight.setTextColor(colorText);
  46. tvRight.setText(textRight);
  47.  
  48. }
  49.  
  50.  
  51. }

my_view.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="10dp" tools:background="@android:color/holo_blue_dark">
  3.  
  4. <TextView android:id="@+id/tv_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" tools:text="left" tools:textColor="#fff"/>
  5.  
  6. <TextView android:id="@+id/tv_title" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textSize="23sp" tools:text="title" tools:textColor="#fff"/>
  7.  
  8. <TextView android:id="@+id/tv_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" tools:text="right" tools:textColor="#fff"/>
  9. </LinearLayout>

3、从第三方命名空间获取自定义属性名称

此处要注意在activity_main.xml要申明第三方命名空间(在android studio中只需要用res-auto,在eclipse中就需要加上完整的包名,如下图)
注:my_view只是使用时的一个名称而已,后方的“http://schemas.android.com/apk/res-auto”才是真正有用的。

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:my_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" >
  3.  
  4. <com.example.double2.viewxmltest.MyView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/holo_blue_dark" my_view:leftText="Back" my_view:rightText="Go" my_view:textColor="#fff" my_view:titleText="MyViewTest" />
  5.  
  6. </RelativeLayout>

最后附上源码:http://download.csdn.net/detail/double2hao/9594621

猜你在找的XML相关文章