Use BaseActivity to implement the menu at the bottom of the home page and exit the program.

Source: Internet
Author: User

Some time ago, in order to get the layout of the project, tabhost was used to keep the bottom menu fixed at the bottom, and activityGroup was used as needed. ActivityGroup is very inconvenient to use. It's good if it's okay !!! For future convenience, I decided to change the main layout. When I thought that the entire project was about to re-lay the layout, I wanted to die ~~~ It took a day to change all the menus at the bottom to radioButton.

The point is coming !!! To make every activity that requires the bottom menu have the bottom menu, I wrote a public layout main. xml and a public BaseActivity. At the same time, in order to exit the implementation of the function, I wrote an AppManager class to manage actiivty.

The Code is as follows:

Main_footer.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal" android:id="@+id/main_linearlayout_footer"android:layout_width="fill_parent" android:layout_height="wrap_content"android:gravity="center_vertical"android:background="@drawable/btm_bar_bg"android:layout_alignParentBottom="true">    <RadioGroup          android:id="@+id/radiogroup_personal_condition"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:orientation="horizontal" >             <RadioButton                 android:id="@+id/homeRadioButton"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginTop="2.0dip"                android:background="@drawable/radio_home"                android:button="@null"                 android:layout_marginLeft="20dp"                 android:layout_marginRight="30dp"                android:tag="1"                />                       <RadioButton                 android:id="@+id/priceRadioButton"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginTop="2.0dip"                android:background="@drawable/radio_price"                android:button="@null"                  android:layout_marginLeft="20dp"                 android:layout_marginRight="30dp"                   android:tag="2"                />                                    <RadioButton                 android:id="@+id/calRadioButton"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginTop="2.0dip"                android:background="@drawable/radio_cal"                android:button="@null"                  android:layout_marginLeft="20dp"                 android:layout_marginRight="30dp"                   android:tag="3"                />                            <RadioButton                 android:id="@+id/newsRadioButton"                 android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginTop="2.0dip"                android:background="@drawable/radio_news"                android:button="@null"                  android:layout_marginLeft="20dp"                 android:layout_marginRight="20dp"                   android:tag="4"                />             </RadioGroup></LinearLayout>

Main. xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#000000"    android:orientation="vertical"><LinearLayout    android:id="@+id/content"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:layout_above="@+id/bottom"    android:layout_alignParentTop="true"    >   </LinearLayout>   <include android:id="@+id/bottom" layout="@layout/main_footer"/></RelativeLayout>

BaseActivity:

/*** Inherited from Activity for future convenient management ** @ author coder **/public class BaseActivity extends Activity {// fixed menu bar radiobuttonprivate Button homeRadioButton; private Button priceRadioButton; private Button newsRadioButton; private Button calRadioButton; private LinearLayout ly_content; // the layout of the content area is private View contentView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestW IndowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. main); ly_content = (LinearLayout) findViewById (R. id. content); homeRadioButton = (Button) findViewById (R. id. homeRadioButton); priceRadioButton = (Button) findViewById (R. id. priceRadioButton); newsRadioButton = (Button) findViewById (R. id. newsRadioButton); calRadioButton = (Button) findViewById (R. id. calRadioButton); homeRadioButton. setOnClickLis Tener (new OnClickListener () {public void onClick (View v) {Intent intent1 = new Intent (BaseActivity. this, HomeActivity. class); startActivity (intent1) ;}}); priceRadioButton. setOnClickListener (new OnClickListener () {public void onClick (View v) {Intent intent2 = new Intent (BaseActivity. this, PriceActivity. class); startActivity (intent2) ;}}); calRadioButton. setOnClickListener (new OnClickListener () {public void o NClick (View v) {Intent intent3 = new Intent (BaseActivity. this, CalendarActivity. class); startActivity (intent3) ;}}); newsRadioButton. setOnClickListener (new OnClickListener () {public void onClick (View v) {Intent intent4 = new Intent (BaseActivity. this, NewsActivity. class); startActivity (intent4) ;}}) ;}/ ***** set the content area ** @ param resId * resource file ID */public void setContentLayout (int resId) {LayoutInflater inflater = (La YoutInflater) getSystemService (Context. LAYOUT_INFLATER_SERVICE); contentView = inflater. inflate (resId, null); LayoutParams layoutParams = new LayoutParams (); contentView. setLayoutParams (layoutParams); // contentView. setBackgroundDrawable (null); if (null! = Ly_content) {ly_content.addView (contentView); // Add Activity to stack AppManager. getAppManager (). addActivity (this); Log. v ("AppManager", "AppManager add actiivty !! "+ This. getLocalClassName () ;}}/***** set the content area ** @ param view * View object */public void setContentLayout (View view) {if (null! = Ly_content) {ly_content.addView (view) ;}/ *** View of the obtained content ** @ return */public View getLyContentView () {return contentView;} public BaseActivity () {}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {/*** 1. if no group is selected, it is set to Menu. NONE 2. id: This is very important: onOptionsItemSelected (MenuItem * item) determines which menu is selected by id 3. define menu arrangement 3. set Title */menu. add (Menu. NONE, 1, Menu. NONE, "exit"); menu. add (Menu. NONE, 2, Menu. NONE, "cancel"); return super. onCreateOptionsMenu (menu) ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {if (item. getItemId () = 1) {// finish (); // System. exit (0); // exit the AppManager program. getAppManager (). appExit (BaseActivity. this);} else if (item. getItemId () = 2) {} return super. onOptionsItemSelected (item);} public void onClick (View v ){}}

AppManager:

Public class AppManager {public static Activity context = null; private static Stack <Activity> activityStack; private static AppManager instance; private AppManager () {}/*** Single instance */public static AppManager getAppManager () {if (instance = null) {instance = new AppManager ();} return instance ;} /*** add Activity to Stack */public void addActivity (Activity activity) {if (activityStack = null) {activityStack = new Stack <Activi Ty> ();} activityStack. add (activity);}/*** get the current Activity (the last pushed in the stack) */public Activity currentActivity () {Activity activity = activityStack. lastElement (); return activity;}/*** ends the current Activity (the last pushed in the stack) */public void finishActivity () {Activity activity = activityStack. lastElement (); finishActivity (activity);}/*** ends the specified Activity */public void finishActivity (Activity activity) {if (activity! = Null) {activityStack. remove (activity); activity. finish (); activity = null;}/*** ends the Activity with the specified Class name */public void finishActivity (Class <?> Cls) {for (Activity activity: activityStack) {if (activity. getClass (). equals (cls) {finishActivity (activity) ;}}/*** end all activities */public void finishAllActivity () {for (int I = 0, size = activityStack. size (); I <size; I ++) {if (null! = ActivityStack. get (I) {activityStack. get (I ). finish () ;}} activityStack. clear ();}/*** exit application */public void AppExit (Context context) {try {finishAllActivity (); ActivityManager activityMgr = (ActivityManager) context. getSystemService (Context. ACTIVITY_SERVICE); activityMgr. restartPackage (context. getPackageName (); System. exit (0);} catch (Exception e ){}}}

For example, HomeActivity inherits the key code of baseActivity: @ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );

// SetContentView (R. layout. home); // call the baseactivity Method
SetContentLayout (R. layout. home );}


:






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.