主页底部菜单用BaseActivity实现及程序的退出功能实现
2013-09-24 17:05
495 查看
前段时间为了弄项目的布局,为了底部菜单一直固定在底部,用了tabhost,接着随着需求又用了activityGroup。activityGroup用得超不方便,没事还是少用为妙!!!为了往后更方便,我决定更改主布局,一想到整个项目要重新弄布局,想死的心也有了~~~用了一天的时间,把底部菜单全部换了radioButton了。
重点来了!!!为了让每个需要底部菜单的activity都能拥有底部菜单,我写了个公有布局main.xml和公用的BaseActivity。同时,为了退出功能的实现,我写了个AppManager类管理actiivty。
代码如下:
@Override
//调用baseactivity的方法
}
重点来了!!!为了让每个需要底部菜单的activity都能拥有底部菜单,我写了个公有布局main.xml和公用的BaseActivity。同时,为了退出功能的实现,我写了个AppManager类管理actiivty。
代码如下:
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:
/** * 继承于Activity用于以后方便管理 * * @author coder * */ public class BaseActivity extends Activity { //固定的菜单栏radiobutton private Button homeRadioButton; private Button priceRadioButton; private Button newsRadioButton; private Button calRadioButton; private LinearLayout ly_content; // 内容区域的布局 private View contentView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(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.setOnClickListener(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 onClick(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); } }); } /*** * 设置内容区域 * * @param resId * 资源文件ID */ public void setContentLayout(int resId) { LayoutInflater inflater = (LayoutInflater) 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); // 添加Activity到堆栈 AppManager.getAppManager().addActivity(this); Log.v("AppManager", "AppManager 添加actiivty!!"+this.getLocalClassName()); } } /*** * 设置内容区域 * * @param view * View对象 */ public void setContentLayout(View view) { if (null != ly_content) { ly_content.addView(view); } } /** * 得到内容的View * * @return */ public View getLyContentView() { return contentView; } public BaseActivity() { } @Override public boolean onCreateOptionsMenu(Menu menu) { /** * 1. 如果不分组,就自定义为Menu.NONE 2. id: 这个很重要:onOptionsItemSelected(MenuItem * item) 根据id来判断那个菜单被选中 3. 定义菜单的排列 3. 设置Title */ menu.add(Menu.NONE, 1, Menu.NONE, "退出"); menu.add(Menu.NONE, 2, Menu.NONE, "取消"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == 1) { //finish(); // System.exit(0); //退出程序 AppManager.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(){} /** * 单一实例 */ public static AppManager getAppManager(){ if(instance==null){ instance=new AppManager(); } return instance; } /** * 添加Activity到堆栈 */ public void addActivity(Activity activity){ if(activityStack==null){ activityStack=new Stack<Activity>(); } activityStack.add(activity); } /** * 获取当前Activity(堆栈中最后一个压入的) */ public Activity currentActivity(){ Activity activity=activityStack.lastElement(); return activity; } /** * 结束当前Activity(堆栈中最后一个压入的) */ public void finishActivity(){ Activity activity=activityStack.lastElement(); finishActivity(activity); } /** * 结束指定的Activity */ public void finishActivity(Activity activity){ if(activity!=null){ activityStack.remove(activity); activity.finish(); activity=null; } } /** * 结束指定类名的Activity */ public void finishActivity(Class<?> cls){ for (Activity activity : activityStack) { if(activity.getClass().equals(cls) ){ finishActivity(activity); } } } /** * 结束所有Activity */ public void finishAllActivity(){ for (int i = 0, size = activityStack.size(); i < size; i++){ if (null != activityStack.get(i)){ activityStack.get(i).finish(); } } activityStack.clear(); } /** * 退出应用程序 */ 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) { } } }
例如HomeActivity继承baseActivity的关键代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.home);
//调用baseactivity的方法
setContentLayout(R.layout.home);
}
效果图:
相关文章推荐
- 微信小程序实现action-sheet弹出底部菜单功能【附源码下载】
- 创建一个学生系统,实现一下功能; * 1:增加学生 * 2:显示学生 * 3:修改学生 * 4:删除学生 * 5:查找学生 * 6:退出程序 * 请输入选择(
- Android中的“再按一次返回键退出程序”功能实现
- android中cordova实现再次点击退出程序功能
- 实现安卓程序退出后重进自己程序的一个小功能(android,unity)
- android 实现home键功能的几种方式,退到后台但不退出程序
- Android编程实现捕获程序异常退出时的错误log信息功能详解
- vue实现底部菜单功能
- 微信公众号WebApp接管返回键实现“再按一次退出程序”功能
- 菜单返回上一级,直接退出和擦屏功能的实现
- Android通过广播事件实现退出程序功能。
- Android之利用广播实现完全退出程序功能
- 安卓项目之淘忆2.0的代码实现之主页底部的导航栏功能
- 程序实现设置IE主页的功能
- android按back键退出程序时,实现“再按一次退出”的功能
- 窗体最大化、最小化、隐藏、关闭功能及程序退出功能的按钮实现
- [置顶] 【Android】双击返回键,退出android应用程序 双击两次退出程序提示功能 三种实现方法
- 微信小程序实现弹出菜单功能
- 实现“再按一次退出程序”的功能
- unity项目实现“再按一次退出程序”提示功能