android 应用架构随笔五(ActionBar与侧滑菜单DrawerLayout)

古之立大事者,不惟有超世之才,亦必有坚忍不拔之志。这篇文章主要讲述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 应用架构随笔五(ActionBar与侧滑菜单DrawerLayout)

文章图片
android 应用架构随笔五(ActionBar与侧滑菜单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>

activity_main.xml分主页和侧滑页两个页面,侧滑页为:
< 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>
start从左到右滑动,end从右到左滑动
继承ActionBarActivity 
android 应用架构随笔五(ActionBar与侧滑菜单DrawerLayout)

文章图片
android 应用架构随笔五(ActionBar与侧滑菜单DrawerLayout)

文章图片
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; } }

BaseActivity setContentView(R.layout.activity_main);
android 应用架构随笔五(ActionBar与侧滑菜单DrawerLayout)

文章图片
android 应用架构随笔五(ActionBar与侧滑菜单DrawerLayout)

文章图片
import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.Menu; import com.heima.googleplay.fragment.AppFragment; import com.heima.googleplay.fragment.BaseFragment; import com.heima.googleplay.fragment.FragmentFactory; import com.heima.googleplay.fragment.GameFragment; import com.heima.googleplay.fragment.HomeFragment; import com.heima.googleplay.utils.UIUtils; import com.heima.googleplay.widget.PagerTab; public class MainActivity extends BaseActivity implements OnPageChangeListener {private PagerTab mTabs; private ViewPager mPager; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }private class MainAdapter extends FragmentStatePagerAdapter{private String[] tab_names; private BaseFragment fragment; public MainAdapter(FragmentManager fm) { super(fm); tab_names = UIUtils.getStringArray(R.array.tab_names); }@Override public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return tab_names[position]; }@Override public Fragment getItem(int position) {return FragmentFactory.createFragment(position); }@Override public int getCount() { // TODO Auto-generated method stub return tab_names.length; }}@Override protected void initView() { setContentView(R.layout.activity_main); mTabs = (PagerTab) findViewById(R.id.tabs); mPager = (ViewPager) findViewById(R.id.pager); MainAdapter adapter = new MainAdapter(getSupportFragmentManager()); mPager.setAdapter(adapter); //绑定到一起 mTabs.setViewPager(mPager); mTabs.setOnPageChangeListener(this); }@Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub}@Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub}@Override public void onPageSelected(int arg0) { BaseFragment fragment = FragmentFactory.createFragment(arg0); fragment.show(); }}

MainActivity 【android 应用架构随笔五(ActionBar与侧滑菜单DrawerLayout)】 

    推荐阅读