Android 側(cè)滑欄

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>

github:
https://github.com/CodeHurricane/SlideDemo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容