DrawerLayout
繼承與ViewGroup,可以當(dāng)作是FramLayout,可以指定其子控件android:layout_gravity屬性設(shè)置左邊側(cè)滑或者右邊側(cè)滑,start是左邊
addDrawerListener
可以通過設(shè)置DrawerListener來監(jiān)聽滑動(dòng)過程中的變化
drawerlayout.addDrawerListener(new DrawerListener() {
@Override
public void onDrawerStateChanged(int newState) {
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
View content = drawerlayout.getChildAt(0);
View menu = drawerView;
float scale = 1-slideOffset;//1~0
float leftScale = (float) (1-0.3*scale);
float rightScale = (float) (0.7f+0.3*scale);//0.7~1
menu.setScaleX(leftScale);//1~0.7
menu.setScaleY(leftScale);//1~0.7
content.setScaleX(rightScale);
content.setScaleY(rightScale);
content.setTranslationX(menu.getMeasuredWidth()*(1-scale));//0~width
}
@Override
public void onDrawerOpened(View drawerView) {
}
@Override
public void onDrawerClosed(View drawerView) {
}
});
與Toolbar組合使用
toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerlayout = (DrawerLayout)findViewById(R.id.drawerlayout);
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerlayout, toolbar,
R.string.drawer_open, R.string.drawer_close);
drawerToggle.syncState();
drawerlayout.addDrawerListener(drawerToggle);//不設(shè)置這里,左邊的圖標(biāo)不會(huì)產(chǎn)生動(dòng)畫
與NavigationView組合使用
可用于快速的制定左面的菜單欄
<android.support.v4.widget.DrawerLayout 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"
tools:context="com.example.lsn8_materialdesign_slidingmenu_navigationview.MainActivity" xmlns:app="http://schemas.android.com/apk/res/com.example.lsn8_materialdesign_slidingmenu_navigationview">
<!-- 內(nèi)容部分 -->
<FrameLayout
android:id="@+id/fl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<!-- 菜單部分 -->
<android.support.design.widget.NavigationView
android:layout_gravity="start"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
app:menu="@menu/navigation_menu"
app:headerLayout="@layout/navigation_headerlayout"
android:background="@android:color/darker_gray"
/>
</android.support.v4.widget.DrawerLayout>
對(duì)應(yīng)menu文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_gallery"
android:title="相冊(cè)"
android:orderInCategory="100"
android:icon="@android:drawable/ic_menu_gallery"
/>
<item
android:id="@+id/action_details"
android:title="詳情"
android:orderInCategory="100"
android:icon="@android:drawable/ic_menu_info_details"
/>
<item
android:id="@+id/action_about"
android:title="關(guān)于"
android:orderInCategory="100"
android:icon="@android:drawable/ic_menu_help"
/>
<item
android:id="@+id/action_music"
android:title="音樂"
android:orderInCategory="100"
android:icon="@android:drawable/ic_menu_more"
>
<menu >
<item
android:id="@+id/action_play"
android:title="播放"
android:icon="@android:drawable/ic_media_play"/>
<item
android:id="@+id/action_pause"
android:title="暫停"
android:icon="@android:drawable/ic_media_pause"/>
</menu>
</item>
</menu>