先写一个ToolBar,再写一个DrawerLayout,Drawerlayout里包括主页面FrameLayout和侧滑页面Relativelayout或者是LinearLayout
activity_main:
<?xml version="1.0" encoding="utf-8"?>custom_toolbar:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.sophieliang.project_android_tiku.MainActivity">
<include layout="@layout/custom_toolbar"></include>
<include layout="@layout/custom_drawerlayout"></include>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:toolbar="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#942929"
toolbar:title="分类练习"
toolbar:titleTextColor="#ffffff">
</android.support.v7.widget.Toolbar>
custom_drawerlayout:
先写FrameLayout再写侧边的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mDrawerLayout"
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">
</FrameLayout>
<RelativeLayout
android:id="@+id/left_menu"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ffffff">
<RelativeLayout
android:id="@+id/rl_imageview"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_alignParentTop="true"
android:background="@mipmap/home_nav_title"
android:gravity="center">
<com.example.sophieliang.project_android_tiku.CircleImageView
android:id="@+id/img_left_menu"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@mipmap/toux" />
<TextView
android:id="@+id/tv_name"
android:layout_below="@id/img_left_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="smallucky"/>
</RelativeLayout>
<RelativeLayout
android:layout_marginBottom="5dp"
android:id="@+id/rl_left_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<LinearLayout
android:id="@+id/setting_left_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="8dp"
android:clickable="true"
android:padding="1dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_settings_grey600_18dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:text="设置" />
</LinearLayout>
<LinearLayout
android:id="@+id/exit_left_menu"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:padding="1dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:src="@mipmap/ic_exit_to_app_grey600_18dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="退出" />
</LinearLayout>
</RelativeLayout>
<ListView
android:id="@+id/lv_left_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/rl_imageview"
android:layout_marginBottom="100dp"
android:layout_marginTop="20dp"
android:divider="@color/gray"
android:dividerHeight="1dp"/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
MainActivity:
@ContentView(value = R.layout.activity_main)
public class MainActivity extends AppCompatActivity{
private List<Map<String,Object>> leftList;
private List<Fragment> fragmentList;
private int[] images;
private String[] titles;
private FragmentManager fm;
private ActionBarDrawerToggle mDrawerToggle;
public static int userId;
@ViewInject(value = R.id.toolBar)
private Toolbar toolbar;
@ViewInject(value = R.id.mDrawerLayout)
private DrawerLayout mDrawerLayout;
@ViewInject(value = R.id.lv_left_menu)
private ListView lv_left_menu;
@ViewInject(value = R.id.setting_left_menu)
private LinearLayout setting_left_menu;
@ViewInject(value = R.id.exit_left_menu)
private LinearLayout exit_left_menu;
@ViewInject(value = R.id.img_left_menu)
private ImageView img_left_menu;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
x.view().inject(this);
Intent intent = getIntent();
userId = intent.getIntExtra("userId", 0);
setSupportActionBar(toolbar);
initListView();
SimpleAdapter adapter = new SimpleAdapter(this,leftList,R.layout.item_left_menu,new String[]{"image","title"},new int[]{R.id.image_left_menu,R.id.title_left_menu});
lv_left_menu.setAdapter(adapter);
initFragment();
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,toolbar, R.string.drawer_open, R.string.drawer_close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
mDrawerLayout.setDrawerShadow(R.mipmap.right_shadow, GravityCompat.START);
Toast.makeText(MainActivity.this, "开", Toast.LENGTH_SHORT).show();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
Toast.makeText(MainActivity.this, "关", Toast.LENGTH_SHORT).show();
}
};
mDrawerToggle.syncState();
mDrawerLayout.setDrawerListener(mDrawerToggle);
fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.content_frame,fragmentList.get(0)).commit();
lv_left_menu.setOnItemClickListener(new DrawerItemClickListener());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.search){
fm.beginTransaction().replace(R.id.content_frame,fragmentList.get(1)).commit();
}
return super.onOptionsItemSelected(item);
}
//初始化FragmentList
private void initFragment() {
fragmentList = new ArrayList<Fragment>();
fragmentList.add(new Fragment_gridview());
fragmentList.add(new Fragment_search());
fragmentList.add(new Fragment_achieve());
fragmentList.add(new Fragment_collecting(userId));
}
//初始化listview菜单列表 添加数据
private void initListView() {
leftList = new ArrayList<Map<String,Object>>();
images = new int[]{R.mipmap.home_nav_icon01,R.mipmap.home_nav_icon02,R.mipmap.home_nav_icon03,R.mipmap.home_nav_icon04};
titles = new String[]{"分类练习","题目查找","我的成就","我的收藏"};
for(int j = 0; j< images.length; j++){
Map<String,Object> map = new HashMap<String,Object>();
map.put("image", images[j]);
map.put("title", titles[j]);
leftList.add(map);
}
}
//当点击左面菜单栏的时候显示右面的Fragment
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//切换主视图区域的fragment
selectItem(i);
}
}
//切换主视图区域的fragment
private void selectItem(int i) {
toolbar.setTitle(titles[i]);
fm.beginTransaction().replace(R.id.content_frame,fragmentList.get(i)).commit();
mDrawerLayout.closeDrawer(Gravity.LEFT);
}
@Event(value = {R.id.setting_left_menu,R.id.exit_left_menu,R.id.img_left_menu},type = View.OnClickListener.class)
private void btnClick(View view){
switch (view.getId()){
case R.id.setting_left_menu:{
// mDrawerLayout.closeDrawer(Gravity.LEFT);
Intent it = new Intent(MainActivity.this,SettingActivity.class);
startActivity(it);
}
break;
case R.id.exit_left_menu:{
finish();
}
break;
}
}
}