Android custom control interface title navigation and control packaging and sharing
Controls
The contents of this article are as follows: 1. custom Control attribute Definition 2. java code of the custom control 3. usage of custom control properties 4. control Project packaging 5. use of other projects
1. Define custom control attributes
Custom control attributes are mainly defined in the attrs. xml file under the values folder.
2. java code of the custom control
The TypedArray class is used to obtain attribute values.
As follows:
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);
The main code of the control is as follows:
/** *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. Usage of custom control attributes
In the layout file, you must first define the address of the referenced resource.
Xmlns: example = http://schemas.android.com/apk/res/com.example.lvCom. example. lvThis is the package name of your project.
4. Control Project Packaging
The Activity, menu, and other unnecessary files contained in the control project can be deleted. Right-click the project properties, as shown in the following figure:
This project can be included in the new project:
Finally, be sure to use the control in the project where the resource is referenced. Enter the package of the project as the resource address.