首先介绍一下动作条,ActionBar就是活动最顶部的那个标题栏。但只能位于活动的顶部,多有不便。Google便又推出了ToolBar,它继承了ActionBar的所有功能,并且灵活性高。ToolBar一般会有个主题,在style.xml中定义。
动作项一般为ToolBar中的按钮或文本,在menu文件布局中定义,通过
onCreateOptionsMenu(Menu menu)方法,为动作条增加动作项,然后再调用onOptionsItemSelected(MenuItem item) ,实现动作的监听
菜单布局
红圈就是菜单布局中定义的创建订单。白圈是定义的隐藏菜单项,内有设置和分享功能
再介绍下DrawerLayout,抽屉布局,就是将一些菜单选项隐藏起来。在主布局中定义,一般,此布局由两部分组成,一个
android:layout_gravity="start"是必须的,因为要知道
列表布局布局到底是从屏幕左边加载还是右边加载,要不然就会出现闪退的错误。DrawerLayout布局如下:
活动启动时的主界面。左边的三横按钮是抽屉布局的图标。可以看到界面显示了Top Fragment,这是由代码控制默认显示的。
当我们点击抽屉按钮时,其隐藏的子片段会显示出来,这里用列表定义了4个子片段。然后每个子片段有自己的ID号和详细内容
再附上主方法的源代码
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ShareActionProvider;
public class MainActivity extends Activity {
private ShareActionProvider shareActionProvider;//共享动作私有变量
private String[] titles;
private ListView drawerList;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private int currentPosition = 0;
private class DrawerItemClickListener implements ListView.OnItemClickListener {
//创建一个监听器
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
selectItem(position);//单击一项是调用此方法
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titles = getResources().getStringArray(R.array.titles);//获得标题数组
drawerList = (ListView) findViewById(R.id.drawer);//抽屉列表布局
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);//抽屉布局,一个片段+列表布局
drawerList.setAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, titles));//初始化抽屉列表
drawerList.setOnItemClickListener(new DrawerItemClickListener());//响应列表布局的单击事件
if (savedInstanceState != null) {
currentPosition = savedInstanceState.getInt("position");//保存当前位置
setActionBarTitle(currentPosition);//活动被撤销后,重新创建,保留前一个状态的标题
} else {
selectItem(0);//初次创建活动是,选择TopFragment
}
//create the ActionBarDrawerToggle,特殊类型的抽屉监听器
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
R.string.open_drawer, R.string.close_deawer) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();//显示菜单项有变化
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(drawerToggle);//设置监听器
getActionBar().setDisplayHomeAsUpEnabled(true);//向上按钮
getActionBar().setHomeButtonEnabled(true);//
//后退堆栈改变时,调用此方法
getFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag("visible_fragment");
if (fragment instanceof TopFragment) {
currentPosition = 0;
}
if (fragment instanceof PizzaFragment) {
currentPosition = 1;
}
if (fragment instanceof PastaFragment) {
currentPosition = 2;
}
if (fragment instanceof StoresFragment) {
currentPosition = 3;
}
setActionBarTitle(currentPosition);//设置动作条标题
drawerList.setItemChecked(currentPosition, true);
}
});
}
//切换帧布局中的片段,单击ListVIew中的一项时要调用此方法
private void selectItem(int position) {
currentPosition = position;
Fragment fragment;
switch (position) {//所点击的项在ListView中的位置
case 1:
fragment = new PizzaFragment();
break;
case 2:
fragment = new PastaFragment();
break;
case 3:
fragment = new StoresFragment();
break;
default:
fragment = new TopFragment();
}
//切换片段事物
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment,"visible_fragment");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();
setActionBarTitle(position);//设置动作条标题
//当单击了某个列表项时,列表布局关闭
drawerLayout.closeDrawer(drawerList);
}
//改变动作条标题
private void setActionBarTitle(int position) {
String title;
if (position == 0) {
title = getResources().getString(R.string.app_name);
} else {
title = titles[position];
}
getActionBar().setTitle(title);//显示动作条的标题
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("position", currentPosition);
}
/// called whenever 调用了invalidateOptionsMenu()
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
//如果抽屉打开,则设置共享动作为false
boolean drawerOpen = drawerLayout.isDrawerOpen(drawerList);
menu.findItem(R.id.action_share).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
/*
将菜单资源文件中的菜单项增加到动作条中,即为动作条增加动作项
*/
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);//R.menu.menu_main是菜单资源文件,menu是动作条
MenuItem menuItem = menu.findItem(R.id.action_share);
//得到共享动作提供者的一个引用
shareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
setIntent("This is example text");//默认文本
return super.onCreateOptionsMenu(menu);
}
/*
setIntent(String text)会创建·一个·意图,并通过
shareActionProvider.setShareIntent(intent)来传递这个意图
*/
private void setIntent(String text) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, text);
shareActionProvider.setShareIntent(intent);
}
@Override
//响应动作条中动作,item是单击的动作条上单击的动作项,
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;//让ActionBarDrawerToggle处理单击事件
}
switch (item.getItemId()) {//获得所单击动作项的ID
case R.id.action_create_order:
Intent intent = new Intent(this, OrderActivity.class);
startActivity(intent);
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
//syncState()方法保证抽屉图标的状态和抽屉布局状态一致
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
//如股配制发生改变,Call 会使配制变化传递到ActionBarDrawerToggle
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
}
详情请参考https://dogriffiths.github.io/HeadFirstAndroid/#/
新手小白学习,如有错误,请多多指正!