android 应用架构随笔五(ActionBar与侧滑菜单DrawerLayout)
ActionBar(V7)的添加非常简单,只需要在AndroidManifest.xml中指定Application或Activity的theme是Theme.Holo或其子类就可以了,在Android 3.0及更高的版本中,Activity中都默认包含有ActionBar组件。
drawerLayout(V4)是Support Library包中实现了侧滑菜单效果的控件,可以说drawerLayout是因为第三方控件如MenuDrawer等的出现之后,google借鉴而出现的产物。drawerLayout分为侧边菜单和主内容区两部分,侧边菜单可以根据手势展开与隐藏(drawerLayout自身特性),主内容区的内容可以随着菜单的点击而变化(这需要使用者自己实现)。
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent"> <com.heima.googleplay.widget.PagerTab android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="42dip" android:background="@drawable/bg_tab"/> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/tabs" tools:context=".MainActivity"/> </RelativeLayout> <FrameLayout android:id="@+id/start_drawer" android:layout_width="250dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="@drawable/bg_tab"></FrameLayout> </android.support.v4.widget.DrawerLayout>
分主页和侧滑页两个页面,侧滑页为:
<FrameLayout
android:></FrameLayout>
start从左到右滑动,end从右到左滑动
继承ActionBarActivity
import android.app.Application; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; public abstract class BaseActivity extends ActionBarActivity { private static BaseActivity mForegroundActivity; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); initView(); } protected abstract void initView() ; @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); this.mForegroundActivity = this; } public static BaseActivity getForegroundActivity(){ return mForegroundActivity; } }