activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--toolBar--> <include layout="@layout/custom_toolbar"></include> <!--DrawerLayout--> <include layout="@layout/custom_layout"></include> </LinearLayout>
custom_layout文件
<?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" android:layout_width="match_parent" android:layout_height="match_parent"> <!--主布局--> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher"/> </LinearLayout> <!--侧滑布局--> <LinearLayout android:layout_width="100dp" android:layout_height="match_parent" android:background="#ccc" android:layout_gravity="left"> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@null"> </ListView> </LinearLayout> </android.support.v4.widget.DrawerLayout>
custom_toolbar文件
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#0000aa"> </android.support.v7.widget.Toolbar>
主函数中
package com.yztc.toolbar02; import android.graphics.Color; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Toolbar mToolbar; private DrawerLayout mDrawerLayout; private ListView lv; private String[] lvs={"我的订单","我的收藏","配置管理","设置","关于我们"}; private ArrayAdapter<String> adapter; private ActionBarDrawerToggle toggle; private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); mToolbar.setTitle("ToolBar");//设置标题 mToolbar.setTitleTextColor(Color.RED);//设置标题的颜色 setSupportActionBar(mToolbar); // 设置向上导航的图标可用 getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); /** * 创建实现抽屉的打开和关闭 mDrawerLayout布局中的抽屉与mToolbar中的向上导航关联 * ActionBarDrawerToggle 是DrawerLayout.DrawerListener实现 */ toggle=new ActionBarDrawerToggle(this,mDrawerLayout,mToolbar, R.string.open,R.string.close){ @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); Log.i("tag","-----onDrawerOpened---"); } @Override public void onDrawerClosed(View drawerView) {super.onDrawerClosed(drawerView);} @Override public void onDrawerStateChanged(int newState) { super.onDrawerStateChanged(newState); } }; toggle.syncState(); mDrawerLayout.setDrawerListener(toggle); //适配抽屉中listview数据 adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,lvs); lv.setAdapter(adapter); } //初始化控件的方法 public void initView(){ mToolbar= (Toolbar) findViewById(R.id.toolbar); mDrawerLayout= (DrawerLayout) findViewById(R.id.drawer); lv= (ListView) findViewById(R.id.lv); iv= (ImageView) findViewById(R.id.iv); }}