前段時間為了弄項目的布局,為了底部菜單一直固定在底部,用了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 {//固定的功能表列radiobuttonprivate Button homeRadioButton;private Button priceRadioButton;private Button newsRadioButton;private Button calRadioButton;private LinearLayout ly_content;// 內容地區的布局private View contentView;@Overrideprotected 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() {}@Overridepublic 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);}@Overridepublic 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);}
: