给自定义View添加xml属性

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

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

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

1、自定义属性名称

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

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

主要代码:

1、自定义属性名称

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

atts.xml中代码如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyTitle">
        <attr name="textColor" format="color"/>
        <attr name="titleText" format="string"/>
        <attr name="leftText" format="string"/>
        <attr name="rightText" format="string"/>
    </declare-styleable>
</resources>

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

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

package com.example.double2.viewxmltest;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

/** * 项目名称:ViewXmlTest * 创建人:Double2号 * 创建时间:2016/8/4 10:23 * 修改备注: */
public class MyView extends LinearLayout {

    private int colorText;
    private String textLeft;
    private String textTitle;
    private String textRight;
    private TextView tvLeft;
    private TextView tvTitle;
    private TextView tvRight;

    public MyView(Context context,AttributeSet attrs,int defStyleAttr) {
        super(context,attrs,defStyleAttr);

        //从xml的属性中获取到字体颜色与string
        TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.MyTitle);
        colorText=ta.getColor(R.styleable.MyTitle_textColor,Color.BLACK);
        textLeft=ta.getString(R.styleable.MyTitle_leftText);
        textTitle=ta.getString(R.styleable.MyTitle_titleText);
        textRight=ta.getString(R.styleable.MyTitle_rightText);
        ta.recycle();

        //获取到控件
        //加载布局文件,与setContentView()效果一样
        LayoutInflater.from(context).inflate(R.layout.my_view,this);
        tvLeft=(TextView)findViewById(R.id.tv_left);
        tvTitle=(TextView)findViewById(R.id.tv_title);
        tvRight=(TextView)findViewById(R.id.tv_right);

        //将控件与设置的xml属性关联
        tvLeft.setTextColor(colorText);
        tvLeft.setText(textLeft);
        tvTitle.setTextColor(colorText);
        tvTitle.setText(textTitle);
        tvRight.setTextColor(colorText);
        tvRight.setText(textRight);

    }


}

my_view.xml

<?xml version="1.0" encoding="utf-8"?>
<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">

    <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"/>

    <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"/>

    <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"/>
</LinearLayout>

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

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

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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" >

    <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" />

</RelativeLayout>

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 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轻松学习总节篇