Android Netease top navigation bar demo

Source: Internet
Author: User

As time passes, the software requires more and more content to be displayed, so to better display more content on a small screen, we will first think of the bottom menu bar, however, sometimes I want Netease news to display too much content and display all the content on the home page. Therefore, the top navigation bar is added, but the memory of mobile devices such as Android is limited, therefore, if multiple interfaces are cached in the memory, memory overflow may easily occur. This is fatal and therefore requires consideration. Although I have also worked on the top navigation bar of Netease before, which method is not good, just like using viewpager to do some complicated interfaces. Because the image occupies too much memory, it is easy to cause memory overflow, after learning about today's content, I believe you will have some experience.

Let's take a look at the results to be achieved today:


You can adjust the image and layout used in the top navigation.

As we have already introduced the bottom menu bar, some repetitive code will not be pasted. In the end, I will also paste it to anyone who is interested in downloading it on their own.

First, let's look at some layout files in the top navigation bar:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: orientation = "vertical"> <include layout = "@ layout/head"/> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"> <radiogroup Android: id = "@ + ID/add_tab_group" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: gravity = "center" Android: paddingtop = "6dp" Android: paddingbottom = "6dp" Android: Background = "@ drawable/big_button_up" Android: Orientation = "horizontal"> <radiobutton Android: id = "@ + ID/main_tab_addexam" style = "@ style/mmtabbutton1" Android: layout_weight = "1.0" Android: checked = "true" Android: TEXT = "add exam"/> <radiobutton Android: Id = "@ + ID/main_tab_myexam" style = "@ style/mmtabbutton1" Android: layout_weight = "1.0" Android: TEXT = "my exams"/> <radiobutton Android: Id = "@ + ID/main_tab_message" style = "@ style/mmtabbutton1" Android: layout_weight = "1.0" Android: TEXT = "My notifications"/> <radiobutton Android: Id = "@ + ID/main_tab_testing" style = "@ style/mmtabbutton1" Android: layout_weight = "1.0" Android: TEXT = "test"/> <radiobutton Android: Id = "@ + ID/main_tab_settings" style = "@ style/mmtabbutton1" Android: layout_weight = "1.0" Android: TEXT = "set"/> </radiogroup> </linearlayout> <linearlayout Android: Id = "@ + ID/container" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: layout_weight = "1"> </linearlayout>

You can adjust the specific width and style by yourself, and then take a look at the core class:

Import android. app. activitygroup; import android. app. alertdialog; import android. app. localactivitymanager; import android. content. context; import android. content. dialoginterface; import android. content. intent; import android. OS. bundle; import android. view. keyevent; import android. view. view; import android. view. animation. animation; import android. view. animation. animationutils; import android. widget. button; impo RT android. widget. linearlayout; import android. widget. radiogroup; import android. widget. textview; import android. widget. radiogroup. extends; public class addexamactivity extends activitygroup {protected button btn_lefttop, btn_righttop; protected textview TV _head; Private Static localactivitymanager manager; private radiogroup; Private Static linearlayout container; public static Context context; @ overrideprotected void oncreate (bundle savedinstancestate) {// todo auto-generated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. addexam); Context = This; inithead (); Manager = getlocalactivitymanager (); Container = (linearlayout) findviewbyid (R. id. container); radiogroup = (radiogroup) This. findviewbyid (R. id. add_tab_group); container. removeallviews (); container. addview (Manager. startactivity ("page_0", new intent (context, myexamactivity. class ). addflags (intent. flag_activity_clear_top )). getdecorview (); radiogroup. setoncheckedchangelistener (New oncheckedchangelistener () {@ overridepublic void oncheckedchanged (radiogroup group, int checkedid) {// todo auto-generated method stubswitch (checkedid) {case R. id. main_tab_addexam: // Add exam container. removeallviews (); container. add View (Manager. startactivity ("page_0", new intent (context, myexamactivity. class ). addflags (intent. flag_activity_clear_top )). getdecorview (); break; case R. id. main_tab_myexam: // my exam container. removeallviews (); container. addview (Manager. startactivity ("page_1", new intent (context, mymessageactivity. class ). addflags (intent. flag_activity_clear_top )). getdecorview (); break; case R. id. main_tab_message: // my notification containe R. removeallviews (); container. addview (Manager. startactivity ("page_2", new intent (context, settingactivity. class ). addflags (intent. flag_activity_clear_top )). getdecorview (); break; case R. id. main_tab_testing: // test container. removeallviews (); container. addview (Manager. startactivity ("page_3", new intent (context, testingactivity. class ). addflags (intent. flag_activity_clear_top )). getdecorview (); break; CAS E r. id. main_tab_settings: // set container. removeallviews (); container. addview (Manager. startactivity ("page_4", new intent (context, myexamactivity. class ). addflags (intent. flag_activity_clear_top )). getdecorview (); break; default: // tabhost. setcurrenttabbytag ("My exams"); break ;}}) ;}public static void changeto () {animation slideleftin = animationutils. loadanimation (context, R. anim. slide_bottom_in_no_alpha ); Container. removeallviews (); container. addview (Manager. startactivity ("page_4", new intent (context, myexamactivity. class ). addflags (intent. flag_activity_clear_top )). getdecorview (); container. startanimation (slideleftin);} protected void inithead () {btn_lefttop = (button) findviewbyid (R. id. btn_lefttop); btn_righttop = (button) findviewbyid (R. id. btn_righttop); TV _head = (textview) findviewbyid (R. id. TV _ Head); btn_lefttop.setvisibility (view. invisible); TV _head.settext ("add exam") ;}@ overridepublic Boolean onkeydown (INT keycode, keyevent event) {// todo auto-generated method stubif (keycode = keyevent. keycode_back) {alertdialog. builder = new alertdialog. builder (getparent (); builder. setmessage ("are you sure you want to quit? "). Setcancelable (false ). setpositivebutton ("OK", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int ID) {finish (); system. exit (0 );}}). setnegativebutton ("return", new dialoginterface. onclicklistener () {public void onclick (dialoginterface dialog, int ID) {dialog. cancel () ;}}); alertdialog Alert = builder. create (); alert. show (); Return true;} return Super. onkeydown (keycode, event );}}

The activitygroup is inherited here. Friends who have never used it can find it from Baidu.

The localactivitymanager promoter activity is used. Here, context and linearlayout use static. This is because of abnormal demands that I have to do so. I hope you do not set these two variables to static, because the life cycle of static is very long, especially the context should not be set to static, so that the current activity is hard to be destroyed. Actually, you can use tabhost, But I believe everyone understands why tabhost is not used. I will also use it if I don't consider the memory. Haha!


Attached at the end. If you are interested, download it by yourself! Click Open Link

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.