public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private ExpandableListView lvLeftMenu;
private TextView help, guanyu, tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
toolbar.setTitle("Toolbar");//设置Toolbar标题
toolbar.setTitleTextColor(Color.parseColor("#ffffff")); //设置标题颜色
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//创建返回键,并实现打开关/闭监听
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open, R.string.close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
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);
final ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
private String[] title = new String[]{"切换设备", "账户管理"};
private String[][] title_t = new String[][]{
{"设备1", "设备2", "设备3"},
{"账户1", "账户2", "账户3"},
};
TextView getTextView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setPadding(36, 20, 36, 20);
textView.setTextSize(20);
textView.setTextColor(Color.BLACK);
return textView;
}
@Override
public int getGroupCount() {
return title.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return title_t[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return title[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return title_t[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView textView = getTextView();
textView.setTextColor(Color.parseColor("#aaaaaa"));
textView.setText(getChild(groupPosition, childPosition).toString());
ll.addView(textView);
return ll;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
};
lvLeftMenu.setAdapter(adapter);
//去掉自带箭头
lvLeftMenu.setGroupIndicator(null);
lvLeftMenu.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
tv.setText(adapter.getChild(groupPosition, childPosition).toString());
mDrawerLayout.closeDrawers();
return false;
}
});
help.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv.setText("帮助");
mDrawerLayout.closeDrawers();
}
});
guanyu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv.setText("关于");
mDrawerLayout.closeDrawers();
}
});
}
private void findViews() {
toolbar = (Toolbar) findViewById(R.id.tl_custom);
mDrawerLayout = (DrawerLayout) findViewById(R.id.dl_left);
lvLeftMenu = (ExpandableListView) findViewById(R.id.lv_left_menu);
help = (TextView) findViewById(R.id.help);
guanyu = (TextView) findViewById(R.id.guanyu);
tv = (TextView) findViewById(R.id.tv);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ldemo.toolbar_drawerlayout_demo.MainActivity">
<include layout="@layout/custom_toolbar" />
<include layout="@layout/custom_drawerlayout" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tl_custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar">
</android.support.v7.widget.Toolbar>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dl_left"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--主布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/tv"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="30sp" />
</LinearLayout>
<!--侧滑菜单-->
<LinearLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:orientation="vertical">
<ExpandableListView
android:id="@+id/lv_left_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@null"
android:text="DrawerLayout" />
<TextView
android:id="@+id/help"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginTop="14dp"
android:text="帮助"
android:textColor="#000"
android:textSize="20sp" />
<TextView
android:id="@+id/guanyu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginTop="14dp"
android:text="关于"
android:textColor="#000"
android:textSize="20sp" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!--状态栏颜色-->
<item name="colorPrimaryDark">@color/Indigo_colorPrimaryDark</item>
<!--Toolbar颜色-->
<item name="colorPrimary">@color/Indigo_colorPrimary</item>
<!--返回键样式-->
<item name="drawerArrowStyle">@style/AppTheme.DrawerArrowToggle</item>
</style>
<style name="AppTheme.DrawerArrowToggle" parent="Base.Widget.AppCompat.DrawerArrowToggle">
<item name="color">@android:color/white</item>
</style>
</resources>
运行效果如下: