Android自定义控件属性

在项目开发的过程中,有一个线性布局在很多地方都使用到了,但是其中的内容又有不同之处,如果每次都写一个布局会产生很多重复多余的代码,使用include包含代码也考虑过,但是也有一些局限性,因此,最后使用了自定义布局。


先来看下实际的效果图



下面就来详细说下整个过程。

先来看下基础的线性布局:

线性布局左边是一个ImageView   然后是一个textView 然后是一个textview  一个imageview固定的箭头图片  布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="45dp"
    android:background="@color/white"
    android:gravity="center_vertical"
    android:orientation="horizontal" >
    
    
    <ImageView 
        android:id="@+id/iv_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dip"
        
        />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_weight="2"
        android:text="@string/my_yuefan"
        android:textColor="#000000"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="5dip"
        android:text="5张"
        android:textColor="@color/gray_txt"
        android:textSize="16sp" />

    <ImageView
        android:layout_width="12dp"
        android:layout_height="12dp"
        android:layout_marginRight="10dp"
        android:src="@drawable/accsessory_arrow_right" />

</LinearLayout>

自定义控件的开发

自定义控件的几个步骤

1.写一个自定义控件类,这个类就是你的自定义控件的实现. 这个类要继承自view,根据实际情况,本例中继承RelativeLayout

package com.baidu.demo.component;

import com.baidu.demo.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MyViewItem extends RelativeLayout {

	private String name;
	private String content;
	private TextView tv_title;
	private TextView tv_content;
	private ImageView iv_icon;
	private int icon;

	public MyViewItem(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		initView(context);
		name = attrs.getAttributeValue(
				"http://schemas.android.com/apk/res/com.junji.richu",
				"itemname");
		content = attrs
				.getAttributeValue(
						"http://schemas.android.com/apk/res/com.baidu.demo",
						"content");
		
		icon = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/com.baidu.demo", "lefticon", 0);
		setTitle(name);
		setContent(content);
		setLeftIcon(icon);
	}

	private void initView(Context context) {
		// TODO Auto-generated method stub
		// 把一个布局文件转化成view对象,挂载在自己身上
		View.inflate(context, R.layout.my_view_item, this);
		tv_title = (TextView) this.findViewById(R.id.tv_title);
		tv_content = (TextView) this.findViewById(R.id.tv_content);
		iv_icon = (ImageView) this.findViewById(R.id.iv_icon);
	}

	/**
	 * 设置标题
	 * 
	 * @param text
	 */
	public void setTitle(String text) {
		tv_title.setText(text);
	}

	/**
	 * 设置图标
	 * 
	 * @param drawableId
	 */
	public void setLeftIcon(int icon) {
		iv_icon.setImageResource(icon);
	}

	/**
	 * 设置内容
	 * 
	 * @param text
	 */
	public void setContent(String text) {
		tv_content.setText(text);
	}

	/**
	 * 获取标题
	 * 
	 * @return
	 */
	public String getTitle() {
		return tv_title.getText().toString();
	}

	/**
	 * 获取内容
	 * 
	 * @return
	 */
	public String getDesc() {
		return tv_content.getText().toString();
	}

}

注意到,其中有itemname content 和lefticon三个属性是通过attrs对象的方法得来的,也就是通过xml文件得到的,这个xml文件在下面的步骤中定义

2.在res/values目录下建立一个attrs.xml的文件,在这个文件中增加对控件的自定义属性的定义.

自定义控件的属性就可以在这个xml文件中定义,name就是你要定义的属性的名称,format是该属性的格式.

<attr>标签的format属性值代表属性的类型,这个类型值一共有10种,分别是:reference,float,color,dimension,boolean,string,enum,integer,fraction,flag

在attrs.xml文件中的<declare-styleable>标签的name属性的值,按照惯例我们都是写成自定义控件类的名字。一个同名的<attr>在attrs.xml中只可以定义一次。

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="MyViewItem">
        <attr name="content" format="string" />
        <attr name="itemname" format="string" />
        <attr name="lefticon" format="reference"/>
    </declare-styleable>

</resources>


3.使用带AttributeSet参数的类的构造函数,并在构造函数中将自定义控件类中变量与attrs.xml中的属性连接起来.


public MyViewItem(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		initView(context);
		name = attrs.getAttributeValue(
				"http://schemas.android.com/apk/res/com.junji.richu",
				"itemname");
		content = attrs
				.getAttributeValue(
						"http://schemas.android.com/apk/res/com.junji.richu",
						"content");
		
		icon = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/com.junji.richu", "lefticon", 0);
		setTitle(name);
		setContent(content);
		setLeftIcon(icon);
	}

4.在自定义控件类中使用这些已经连接的属性变量.



5.将自定义的控件类定义到布局用的xml文件中去.

注意:要使用全路径名

<com.baidu.demo.component.MyViewItem
                android:id="@+id/mv_yuefan"
                android:layout_width="fill_parent"
                android:layout_height="45dip"
                android:layout_marginTop="20dip"
                android:background="@drawable/bg_box"
                junji:lefticon ="@drawable/ic_appointment"
                junji:content=""
                junji:itemname="约饭活动" >
</com.baidu.demo.component.MyViewItem>


6.在界面中生成此自定义控件类对象,就完成了自定义控件的创建和使用了.

在java代码中就可以new这个自定义控件,并且使用其中的方法了,比如本例子中的
setTitle(name);
setContent(content);
setLeftIcon(icon);

这几个方法



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值