Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 自定義控件之界面標題導航及控件打包共享

Android 自定義控件之界面標題導航及控件打包共享

編輯:關於Android編程

 

控件主要如圖所示

\

 

 

 

本文目錄主要如下:

1.自定義控件屬性的定義

2.自定義控件的java代碼

3.自定義控件屬性的用法

4.控件項目的打包處理

5.其他項目的使用

 

 

1.自定義控件屬性的定義

自定義控件屬性主要定義在values文件夾下的attrs.xml文件裡。

 




    
    
    
    
    
    
    
    
    

    
        
        
        
        
        
        
        
        
        
    

 

 

2.自定義控件的java代碼

獲取屬性值主要通過 TypedArray 類來獲取屬性值

如下:

            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);


控件的主要代碼如下:

/**
 *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);
	}
}

 

3.自定義控件屬性的用法

在布局文件裡首先要定義引用資源的地址

xmlns:example=http://schemas.android.com/apk/res/com.example.lv com.example.lv這個是你所在項目的包名

 



     
   

 

4.控件項目的打包處理

控件項目裡面包含的Activity ,menu等等不用的文件可以刪除。然後右擊項目屬性,如下:

\

 

 

在新建的項目裡可以包含這個項目:如下圖

\

 

 

最後一定要注意,在所在項目中使用控件,引用資源的地址一樣要填本項目的包

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved