ToolBar和DrawerLayout和Slide
DrawerLayout抽屉的使用
重点是布局 要给抽屉布局设置android:layout_gravity=""属性
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:background="#CF0E0E">
<Button
android:id="@+id/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:background="#E2C908">
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_gravity="left"
android:layout_width="200dp"
android:layout_height="match_parent"
android:background="#F35E91"
android:orientation="vertical">
<Button
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭"/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记得双击摸摸哒"/>
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
SlideMenu实现抽屉
//实例对象
slidingMenu= new SlidingMenu(this);
//设置抽屉显示的位置
slidingMenu.setMode(SlidingMenu.LEFT);
//设置抽屉的布局
slidingMenu.setMenu(inflate);
SlideMenu的其他属性
设置模式: setMode(SlidingMenu.LEFT);
设置触摸屏幕的模式:setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
//TOUCHMODE_FULLSCREEN全屏;TOUCHMODE_MARGIN边界;TOUCHMODE_NONE不能滑动
设置左侧菜单滑动显示的内容:slidingMenu.setMenu(R.layout.slide_menu);
设置左侧滑动菜单的阴影宽度:slidingMenu.setShadowWidth(300);
设置滑动时的渐变程度:slidingMenu.setFadeDegree(0.5f);范围0.0f-1.0f
设置淡入淡出的效果:slidingMenu.setFadeEnabled(true);
设置左侧滑动菜单的阴影图片(颜色):setShadowDrawable();
设置滑出时主页面显示的剩余宽度:slidingMenu.setBehindOffset(200);
ToolBar
1.现在res/menu/创建menu
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/item1" android:title="item1">lalala</item>
<item android:id="@+id/item2" android:title="item2">hahaha</item>
<item android:id="@+id/item3" android:title="item3">liuliuliu</item>
</menu>
2.将actionbar设置为NoActionBar
//给toolbar设置 布局
toolbar.inflateMenu(R.menu.menu);
//点击事件
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.item1:
Toast.makeText(Main2Activity.this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.item2:
Toast.makeText(Main2Activity.this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.item3:
Toast.makeText(Main2Activity.this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();
break;
}
return true;
}
});