Android Side-Slide menu implementation

Source: Internet
Author: User

have been looking at other people application inside there is a side-slip menu, feel good cool, but I can not write, the mood is called sad, hate themselves will not. I also Baidu for a period of time, only hate their level is not high, do not understand the code, so looked for a lot of examples, but also do not understand, finally a chance, I found, yes, found. This code is very simple, because it provides someone else to write a class, that is, the layout class, directly in the XML can be used, and then can be directly run, as for what to add, with their own mind.

OK, not much to say, now start to talk about the process of implementation, really simple, the sentence equivalent to provide an existing space, all you have to do is to fill in the code, and then you can run. (At that time to see other people, is not his use of this class ah, in fact, everyone is similar)

Let's see:



As shown, the menu is on the left and right, the main interface moves to the right, and the menu slowly comes out, much more cool than the kind of menu Android offers.

Well, what is that class?

Slideholder, which is a subclass of Framelayout, that is, this is used in an XML layout file, such as the following code, which is the layout code of Mainactivity:

<span style= "FONT-SIZE:18PX;" ><com.example.slidemenu_test. Slideholder xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/ Tools "Android:id=" @+id/holder "android:layout_width=" match_parent "android:layout_height=" match_parent "tools : context= ".        Mainactivity "> <scrollview android:layout_width=" 200dp "android:layout_height=" Fill_parent "            android:background= "#000000" > <linearlayout android:layout_width= "fill_parent"                 android:layout_height= "fill_parent" android:orientation= "vertical" > <textview Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" Andro            id:text= "Menu 1" android:textcolor= "#ffffff" android:textsize= "25sp"/>       <textview android:layout_width= "Fill_parent"         android:layout_height= "Wrap_content" android:text= "Menu 2" android:textcolor= "#ffffff" Android:textsize= "25sp"/> </LinearLayout> </ScrollView> <li Nearlayout android:layout_width= "fill_parent" android:layout_height= "Fill_parent" Android:orientatio n= "vertical" android:background= "@drawable/bg" > <linearlayout android:layout_width = "Fill_parent" android:layout_height= "50DP" > <imagebutton android:i                D= "@+id/imagebutton" android:layout_width= "50DP" android:layout_height= "50DP" android:src= "@drawable/btn_icon" android:background= "#0000"/> </ Linearlayout> </linearlayout></com.example.slidemenu_test. Slideholder></span>

Next I explain this code, in the new Android project, Eclipse will automatically generate Activity_main.xml code, it is mainactivity layout file, we need to put activity_ The contents of the Main.xml are replaced with the ones shown above, and then they can be run directly. In the above code, the first is the ScrollView, that is, the menu, we add their own content to this layout, I just add a little bit, as a test, then a layout linearlayout, this is the main interface, that is, do not slide when the display of the interface, In this, I added a button to control the pop-up menu, of course, can not be set, can be run directly, but many applications are set, I also set a ImageButton, I added a mainactivity inside it was set, Let's take a look at Mainactivity's code (easy to understand):

Package Com.example.slidemenu_test;import Android.os.bundle;import Android.app.activity;import android.view.Menu; Import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.imagebutton;public class Mainactivity extends Activity {private ImageButton mbtn;private slideholder mslideholder; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); MBtn = (ImageButton) Findviewbyid (R.id.imagebutton); mslideholder = (Slideholder) Findviewbyid (R.id.holder); Mbtn.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {mslideholder.toggle ();}});}}
In this code, there is a ImageButton object and a Slideholder object, to add events to ImageButton, click on the popup menu, pop-up menu is relatively simple, a line of codeMslideholder.toggle (); function Toggle is the method in Slideholder people want to know more I download the source of the code to learn more about it.

Well, so far, a side-slip menu is out (standing on someone's shoulder, I can see farther ^_^)

Source: http://download.csdn.net/detail/programchangesworld/8589471

Android Side-Slide menu implementation

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.