您的位置:首页 > 其它

主页底部菜单用BaseActivity实现及程序的退出功能实现

2013-09-24 17:05 495 查看
前段时间为了弄项目的布局,为了底部菜单一直固定在底部,用了tabhost,接着随着需求又用了activityGroup。activityGroup用得超不方便,没事还是少用为妙!!!为了往后更方便,我决定更改主布局,一想到整个项目要重新弄布局,想死的心也有了~~~用了一天的时间,把底部菜单全部换了radioButton了。

重点来了!!!为了让每个需要底部菜单的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);

}

效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: