I. Preface
Go straight to the topic. Many Android apps have a menu bar. Apart from the background images and icons, the layout of the menu bar is basically the same. It can be roughly 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.
To consider code reusability, this article will explain how to implement the universal menu bar. The code in the example is slightly changed to meet most software development needs.
Ii. Example
I have always used to the truth. Next let's take a look at the general menu bar:
Iii. Implementation introduction 3.1 menu bar layout file: title_top_view.xml
3.2 MainActivity page layout file: activity_main.xml
3.3java code
When talking about java code, first look at the general menu bar code design class diagram, as shown below:
Class diagram description: This Demo declares the members of the left-side (mLeftView), middle-side (mMidView), and right-side (mRightView) of the menu bar as protected, which violates code encapsulation, you can download the Demo and modify it to private and provide external interfaces. This Demo is intended to facilitate subclass access and provide access speeds.
The BaseActivity. java code is as follows:
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 mLeftVie W; // The left button protected TextView mMidView; // The intermediate text protected ImageView mRightView; // the right button @ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); // sets the title bar requestWindowFeature (Window. FEATURE_NO_TITLE); initView (savedInstanceState) ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubswitch (v. getId () {case R. id. le Ft_btn: {onClickLeftBtn (); break;} case R. id. right_btn: {onClickRigthBtn (); break;} 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) ;}}/*** initialization page * @ param savedInstanceState */protected abstract void initView (Bundle savedInstanceState);/*** click the button on the left of the menu bar, response processing function. Subclass can inherit and implement its own Processing Method */protected abstract void onClickLeftBtn (); protected abstract void onClickRigthBtn ();}
The MainActivity. java code is as follows:
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); // you can specify initTitleBar () in the menu bar. // you can specify setMidTxt (getResources (). getString (R. string. app_name) ;}@ Overrideprotected void onClickLeftBtn () {// TODO Auto-generated method stubToast. makeText (this, "click the button on the left of the menu", Toast. LENGTH_SHORT ). show () ;}@ Overrideprotected void onClickRigthBtn () {// TODO Auto-generated method stubToast. makeText (this, "click the button on the right of the menu", Toast. LENGTH_SHORT ). show ();}}
Iv. Sample download
The Demo sample code download path, http://download.csdn.net/detail/improveyourself/7505935
Ps: If you have a better implementation method, you can leave a message for me. Thank you first.