Android 自定义控件之界面标题导航及控件打包共享

 

控件主要如图所示

 

 

 

本文目录主要如下:

1.自定义控件属性的定义

2.自定义控件的java代码

3.自定义控件属性的用法

4.控件项目的打包处理

5.其他项目的使用

 

 

1.自定义控件属性的定义

        自定义控件属性主要定义在values文件夹下的attrs.xml文件里。

  

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<resources>

    <attr name="drawable_right" />
    <attr name="drawable_left" />
    <attr name="text_left" />
    <attr name="text_middle" />
    <attr name="text_right" />
    <attr name="textSize_left" />
    <attr name="textSize_middle" />
    <attr name="textSize_right" />
    <attr name="bg" />

    <declare-styleable name="CustomHeadView">
        <attr name="drawable_right" />
        <attr name="drawable_left" />
        <attr name="text_left" />
        <attr name="text_middle" />
        <attr name="text_right" />
        <attr name="textSize_left" />
        <attr name="textSize_middle" />
        <attr name="textSize_right" />
        <attr name="bg" />
    </declare-styleable>

</resources></span>


 

      

2.自定义控件的java代码

获取属性值主要通过 TypedArray 类来获取属性值

如下:

<span style="font-size:14px;">            TypedArray typedArray = context.obtainStyledAttributes(attrs,
				R.styleable.CustomHeadView);
		id_bg=typedArray.getInt(R.styleable.CustomHeadView_bg, -1);
		str_TextOfLeft = typedArray
				.getString(R.styleable.CustomHeadView_text_left);
		str_TextOfMiddle = typedArray
				.getString(R.styleable.CustomHeadView_text_middle);
		str_TextOfRight = typedArray
				.getString(R.styleable.CustomHeadView_text_right);
		id_drawOfRight = typedArray.getInt(R.styleable.CustomHeadView_drawable_right,-1);
		id_drawOfLeft = typedArray.getInt(R.styleable.CustomHeadView_drawable_left,-1);
		
		f_textSizeOfLeft = typedArray.getFloat(R.styleable.CustomHeadView_textSize_left, 22);
		f_textSizeOfMiddle = typedArray.getFloat(R.styleable.CustomHeadView_textSize_middle, 22);
		f_textSizeOfRight = typedArray.getFloat(R.styleable.CustomHeadView_textSize_right, 22);</span>


控件的主要代码如下:

<span style="font-size:14px;">/**
 *by lvshujun 2014/9/2
 */
package com.example.customview;



import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class CustomHeadView extends RelativeLayout
{
	
	private int id_bg;
	private int id_drawOfRight;
	private int id_drawOfLeft;
	private String str_TextOfLeft;
	private String str_TextOfMiddle;
	private String str_TextOfRight;
	private float f_textSizeOfLeft;
	private float f_textSizeOfMiddle;
	private float f_textSizeOfRight;
	
	public TextView tv_middle;
	public TextView tv_left;
	public TextView tv_right;
	public CustomHeadView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		
		initData(context,attrs);
		initView(context);
	}

	private void initData(Context context,AttributeSet attrs) {
		// TODO Auto-generated method stub
		TypedArray typedArray = context.obtainStyledAttributes(attrs,
				R.styleable.CustomHeadView);
		id_bg=typedArray.getInt(R.styleable.CustomHeadView_bg, -1);
		str_TextOfLeft = typedArray
				.getString(R.styleable.CustomHeadView_text_left);
		str_TextOfMiddle = typedArray
				.getString(R.styleable.CustomHeadView_text_middle);
		str_TextOfRight = typedArray
				.getString(R.styleable.CustomHeadView_text_right);
		id_drawOfRight = typedArray.getInt(R.styleable.CustomHeadView_drawable_right,-1);
		id_drawOfLeft = typedArray.getInt(R.styleable.CustomHeadView_drawable_left,-1);
		
		f_textSizeOfLeft = typedArray.getFloat(R.styleable.CustomHeadView_textSize_left, 22);
		f_textSizeOfMiddle = typedArray.getFloat(R.styleable.CustomHeadView_textSize_middle, 22);
		f_textSizeOfRight = typedArray.getFloat(R.styleable.CustomHeadView_textSize_right, 22);
	}

	private void initView(Context context) {
		// TODO Auto-generated method stub
		if(id_bg!=-1)
		    setBackgroundResource(id_bg);
		tv_middle = new TextView(context);
		LayoutParams params2 = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
		params2.addRule(RelativeLayout.CENTER_IN_PARENT);
		tv_middle.setLayoutParams(params2);
		tv_middle.setText(str_TextOfMiddle);
		tv_middle.setTextSize(f_textSizeOfMiddle);
		tv_middle.setGravity(Gravity.CENTER);
		addView(tv_middle);
		
		tv_left= new TextView(context);
		LayoutParams params1 = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		params1.addRule(RelativeLayout.CENTER_VERTICAL);
		tv_left.setLayoutParams(params1);
		tv_left.setText(str_TextOfLeft);
		if(id_drawOfLeft!=-1)
		tv_left.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(id_drawOfLeft), null, null, null);
		tv_left.setTextSize(f_textSizeOfLeft);
		addView(tv_left);
		
		tv_right = new TextView(context);
		LayoutParams params3 = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		params3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		params3.addRule(RelativeLayout.CENTER_VERTICAL);
		tv_right.setLayoutParams(params3);
		tv_right.setText(str_TextOfRight);
		tv_right.setTextSize(f_textSizeOfRight);
		tv_right.setGravity(Gravity.CENTER_VERTICAL);
		if(id_drawOfRight!=-1)
		tv_right.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(id_drawOfRight), null);
		addView(tv_right);
	}

	@Override
	protected void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		
	}
	
	public void setViewtOnClickListener(View view,OnClickListener listener)
	{

		view.setOnClickListener(listener);
	}
}
</span>


 

3.自定义控件属性的用法

在布局文件里首先要定义引用资源的地址

xmlns:example="http://schemas.android.com/apk/res/com.example.lvcom.example.lv这个是你所在项目的包名

 

<span style="font-size:14px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:example="http://schemas.android.com/apk/res/com.example.lv"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <com.example.customview.CustomHeadView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/custom"
        example:text_left="首页"
        example:text_middle="主题"
        example:drawable_right="0x7f020000"
        example:bg="0x7f020003"
        example:drawable_left="0x7f020001"
         /> 
   
</LinearLayout>
</span>


 

4.控件项目的打包处理

控件项目里面包含的Activity  ,menu等等不用的文件可以删除。然后右击项目属性,如下:

 

 

在新建的项目里可以包含这个项目:如下图

 

 

最后一定要注意,在所在项目中使用控件,引用资源的地址一样要填本项目的包

xmlns:example="http://schemas.android.com/apk/res/com.example.lv"

 

 

另附,项目下载地址:http://download.csdn.net/detail/q22232222/7860833

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值