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 stubTypedArray 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 stubif(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);}@Overrideprotected 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等等不用的檔案可以刪除。然後右擊項目屬性,如下:
在建立的項目裡可以包含這個項目:如
最後一定要注意,在所在項目中使用控制項,引用資源的地址一樣要填本項目的包