一:MainActivity
package com.example.kirito.myapplication.testdrawlayout;
import android.app.Activity;
import android.app.Fragment;
import android.app.SearchManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
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.Toast;
import com.example.kirito.myapplication.R;
/**
* Created by kirito on 2016.12.17.
*/
public class TestDrawerLayout extends AppCompatActivity {
private DrawerLayout drawerLayout;
private ListView lv;
private ActionBarDrawerToggle toggle;
private CharSequence mTitle;
private CharSequence mDrawerTitle;
private String [] mPlanetTitle;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_layout);
mTitle = mDrawerTitle = getTitle();
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
lv = (ListView) findViewById(R.id.lv);
mPlanetTitle = getResources().getStringArray(R.array.planets_array);//获取values里的string-array
lv.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list_item,mPlanetTitle));
lv.setOnItemClickListener(new DrawerItemClickListener());
toggle = new ActionBarDrawerToggle(this,
drawerLayout,R.mipmap.ic_launcher,R.string.drawer_open,R.string.drawer_close){
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("please choose");
invalidateOptionsMenu();// Call onPrepareOptionsMenu()
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(toggle);
//在左上角显示按钮并且可点击
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
if (savedInstanceState == null){
selectItem(0);
}
}
//重写setTitle方法
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(title);
}
//创建自定义menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return super.onCreateOptionsMenu(menu);
}
//当DrawerLayout打开的时候,隐藏右上角的搜索按钮
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean isOpen = drawerLayout.isDrawerOpen(lv);
menu.findItem(R.id.main).setVisible(!isOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (toggle.onOptionsItemSelected(item)){
return true;
}
switch (item.getItemId()){
case R.id.main://调用手机自带的搜索工具搜索当前title
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY,getSupportActionBar().getTitle());
if (intent.resolveActivity(getPackageManager()) != null){
startActivity(intent);
}else {
Toast.makeText(this,"app not avaliable",Toast.LENGTH_SHORT).show();
}
return true;
default: return super.onOptionsItemSelected(item);
}
}
private class DrawerItemClickListener implements ListView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
//当选择侧边栏(DrawerLayout里的item)触发的点击事件
private void selectItem(int position) {
Fragment fragment = new ContentFragment();
Bundle args = new Bundle();
args.putString("text",mPlanetTitle[position]);
fragment.setArguments(args);
//替换原来的fragment
getFragmentManager().beginTransaction().replace(R.id.content_frame,fragment).commit();
// update selected item and title, then close the drawer
// Highlight the selected item, update the title, and close the drawer
lv.setItemChecked(position,true);
//更换title
mDrawerTitle = mPlanetTitle[position];
drawerLayout.closeDrawer(lv);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
toggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
toggle.onConfigurationChanged(newConfig);
}
}
二:Fragment
package com.example.kirito.myapplication.testdrawlayout;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.kirito.myapplication.R;
/**
* Created by kirito on 2016.12.17.
*/
public class ContentFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frame_layout,container,false);
TextView tv = (TextView) view.findViewById(R.id.tv_drawer);
tv.setText(getArguments().getString("text"));
return view;
}
}
三:自定义menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/main"
android:icon="@drawable/search"
android:title="select"
app:showAsAction="ifRoom|withText"
/>
</menu>
四:布局文件
1)主布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ListView
android:id="@+id/lv"
android:choiceMode="singleChoice"
android:layout_gravity="start"
/*The drawer view (the ListView) must specify its horizontal gravity with the android:layout_gravity attribute. To support right-to-left (RTL) languages,
specify the value with "start" instead of "left"
(so the drawer appears on the right when the layout is RTL).*/
android:divider="@android:color/transparent"
android:layout_width="240dp"
android:layout_height="match_parent"/>
</android.support.v4.widget.DrawerLayout>
2)fragment布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_drawer"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
3)Drawer list item布局文件
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#fff"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
显示效果: