Implementation of the top menu bar Title of Android

Source: Internet
Author: User

I just introduced how to implement the menu bar at the bottom. Next I will introduce the implementation at the top.

I encapsulated all the troublesome content into classes and inherited them directly when using them. If you want to know the principles, refer to the source code ~~ Okay, let's not talk about it. serving food !!

First, you need to introduce two classes, BaseLayout and BaseTitleActivity. This is also the two classes that I want to give you. When you need to have the top menu bar, your Activity needs to inherit the BaseTitleActivity class, and then you will be prompted to override the HandleTitleBarEvent () method in BaseTitleActivity. This is to control the button click event in the top menu bar, And BaseLayout inherits RelativeLayout, so BaseLayout needs to introduce its layout file. I name it titlebar. xml, you only need to copy it to the layout folder, and the rest do not need to worry about it ~

At this time, you do not need any Code except creating a new Activity to inherit BaseTitleActivity. When writing code, from now on, find the Activity that inherits BaseTitleActivity. Here I am MainActivity, change the setContentView function to setView. Correct, because I changed it to setView in BaseTitleActivity. The Code is as follows:

At this time, you only need to call two functions to set the top menu bar you like. setTitle () is easy to understand. It sets the text displayed on the top and

SetTitleBar (int, int,); Two int parameters represent the id of the image to be passed in. You can use R. drawable. ** to obtain the image in the drawable folder. The Code is as follows:

 

Package com. example. topmenu; import com. sniper. use. baseTitleActivity; import android. OS. bundle; import android. app. activity; import android. view. menu; public class MainActivity extends BaseTitleActivity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setView (R. layout. activity_main); SetTitle (Successful !!); SetTitleBar (R. drawable. title_back, R. drawable. title_home,) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Overrideprotected void HandleTitleBarEvent (int buttonId) {// TODO Auto-generated method stub }}


 

As follows:

 

Wait. It seems that no button clicking event has been added yet? Do you still remember to rewrite a HandleTitleBarEvent method? The parameter buttonId = 0 indicates the button on the left, and buttonId = 1 indicates the button on the right, so you can define the event in this way.

 

@ Overrideprotected void HandleTitleBarEvent (int buttonId) {// TODO Auto-generated method stubswitch (buttonId) {case 0: showToast (click the button on the left); case 1: showToast (click the button on the right );}}


 

Last:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.