Android 自訂控制項之介面標題導航及控制項打套件共用

來源:互聯網
上載者:User

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等等不用的檔案可以刪除。然後右擊項目屬性,如下:

 

 

在建立的項目裡可以包含這個項目:如

 

 

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

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.