Introduction of Preface
Go straight to the theme, many Android apps have a menu bar, menu bar In addition to background pictures, icons, the layout is basically the same. It can be broadly divided into three parts: the left area of the menu bar, the middle area of the menu bar, and the right area of the menu bar.
In order to consider the reusability of code, this article will explain the implementation of the common menu bar. The code in the example is slightly flexible enough to meet most of the software development needs.
Ii. Examples
My usual habit is to have a picture of the truth. Let's look at the General menu bar:
III. Introduction of Implementation 3.1menu Bar Layout file: Title_top_view.xml
<span style= "FONT-SIZE:14PX;" ><?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/ Res/android "android:layout_width=" match_parent "android:layout_height=" match_parent "android:background=" #000 " > <relativelayout android:id= "@+id/title_bar" android:layout_width= "Fill_parent" Android:lay out_height= "Wrap_content" android:background= "@drawable/title_bg" > <!--left area and < ; ImageButton android:id= "@+id/left_btn" android:layout_width= "Wrap_content" android:layout_height= "W Rap_content "android:layout_alignparentleft=" true "android:layout_centervertical=" true "a ndroid:layout_marginleft= "5dip" android:background= "@drawable/select_back"/> <!--Middle area--& Gt <textview android:id= "@+id/mid_txt" android:layout_width= "Wrap_content" Android:layout_height= "Wrap_content" android:layout_centerinparent= "true" Android:singleline= "true" Android Oid:ellipsize= "End" android:layout_marginleft= "60dip" android:layout_marginright= "60dip" /> <!--Right area--<imagebutton android:id= "@+id/right_btn" Android:layout_wid Th= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentright= "true" android:layout_marginright= "5dip" android:layout_centervertical= "true" android:background= "@dra Wable/selector_setting "/> </RelativeLayout></RelativeLayout></span>
3.2 mainactivity Page layout file: Activity_main.xml
<span style= "FONT-SIZE:14PX;" ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " tools:context= ". Mainactivity > <!--to import the menu bar- <include android:id= "@+id/title_bar" Android : layout_width= "fill_parent" android:layout_height= "wrap_content" layout= "@layout/title_top_view"/> <textview android:layout_below= "@id/title_bar" android:layout_width= "Wrap_content " android:layout_height= "Wrap_content" android:text= "@string/hello_world"/></relativelayout></ Span>
3.3java Code Section
referring to the Java Code section, first look at the common menu bar code design class diagram, as follows:
Class Diagram Description: This demo will be the left area of the menu bar (Mleftview), the middle area (mmidview), the right area (Mrightview) members are declared as protected, there is a violation of the code encapsulation, you can download the demo self-modified to private, and provide external interface. The main purpose of this demo is to facilitate sub-class access and provide access speed.
The Baseactivity.java code is as follows:
<span style= "FONT-SIZE:14PX;" >package com.example.titledemo;import android.app.activity;import Android.os.bundle;import Android.view.View; Import Android.view.view.onclicklistener;import Android.view.window;import Android.widget.imageview;import Android.widget.textview;import Android.widget.toast;public abstract class Baseactivity extends Activity implements Onclicklistener {protected View mtitleview;protected ImageView mleftview;//left button protected TextView mmidview;// Middle text protected ImageView mrightview;//right button @overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO Auto-generated method Stubsuper.oncreate (savedinstancestate);//Set title bar Requestwindowfeature (window.feature_no_title ); Initview (savedinstancestate);} @Overridepublic void OnClick (View v) {//TODO auto-generated method Stubswitch (V.getid ()) {case R.ID.LEFT_BTN: {onclickle Ftbtn (); break;} Case R.ID.RIGHT_BTN: {onclickrigthbtn (); Default: {break;}}} /** * Initialize menu bar */protected void Inittitlebar () {Mtitleview = FindvIewbyid (R.id.title_bar); if (Mtitleview! = null) {mtitleview.setvisibility (view.visible); Mleftview = (ImageView) Findviewbyid (R.ID.LEFT_BTN); Mleftview.setonclicklistener (this); Mmidview = (TextView) Findviewbyid (r.id.mid_txt); Mrightview = (ImageView) Findviewbyid (R.ID.RIGHT_BTN); Mrightview.setonclicklistener (this);}} /** * Set Intermediate text */protected void Setmidtxt (String strtxt) {if (Mmidview! = null) {Mmidview.settext (strtxt);}} /** * Initialize page * @param savedinstancestate */protected abstract void Initview (Bundle savedinstancestate);/** * Click the Left button on the menu bar to respond to processing function, subclasses can inherit to implement their own processing mode */protected abstract void onclickleftbtn ();p rotected abstract void onclickrigthbtn ();} </span>
The Mainactivity.java code is as follows:
<span style= "FONT-SIZE:14PX;" >package Com.example.titledemo;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.window;import Android.widget.toast;public class MainActivity Extends Baseactivity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate );} @Overrideprotected void Initview (Bundle savedinstancestate) {//TODO auto-generated method Stubsetcontentview ( R.layout.activity_main);//Set menu bar Inittitlebar ();//Set menu middle text value setmidtxt (getresources (). getString (R.string.app_name) );} @Overrideprotected void Onclickleftbtn () {//TODO auto-generated method Stubtoast.maketext (This, "Click on the Menu left button", Toast.length_short). Show ();} @Overrideprotected void Onclickrigthbtn () {//TODO auto-generated method Stubtoast.maketext (This, "Click on the Menu Right button", Toast.length_short). Show ();}} </span>
Iv. Sample Download
The following is the Demo sample code download path, http://download.csdn.net/detail/improveyourself/7505935
PS: If you have a better way to achieve, you can give me a message, in this first thank you.