第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

高大上的側(cè)滑菜單DrawerLayout,解決了不能全屏滑動(dòng)的問(wèn)題

標(biāo)簽:
Android

自从2014那年谷歌提出的Material Design后,这种设计语言就广泛被程序猿使用,屡试不爽。在现如今的各个流行APP中,你都能发现它的身影。详细情况,自己百度吧,我只想说很装B。


今天我就说一下其中的一个控件 DrawerLayout。在此之前,我一直用的是SlidingMenu,虽然体验也不错,但是也有一些bug...比如不能修改成沉浸式状态栏,后来有位大神跟我说如何修改,但对其印象就不是很好。当我接触到DrawerLayout,发现它用起来简单,方便,毕竟是谷歌亲儿子。

DrawerLayout预览

DrawerLayout主要功能就是 实现侧滑菜单效果的功能,并且可以通过增加一些设置来实现高大上的效果,那么就请看动态图:

注意左上角那个图标,有木有很好玩,哈哈...

接下来就介绍如何实现这一功能

1. 在项目对应的build.gradle中添加依赖

    dependencies {
        ...//其他代码
        compile 'com.android.support:appcompat-v7:24.0.0'
        compile 'com.android.support:design:24.0.0'
           ...//其他代码
    }

2. 添加ToolBar,创建toolbar.xml文件

    <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:clipToPadding="true"
            android:fitsSystemWindows="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:title="资讯"
            app:titleTextColor="#fff">
        </android.support.v7.widget.Toolbar>
    </RelativeLayout>

3. 在main.xml中添加DrawerLayout

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- 添加ToolBar -->
        <include layout="@layout/toolbar"/>

        <!--添加DrawerLayout-->
        <android.support.v4.widget.DrawerLayout
            android:id="@+id/drawerlayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <!-- 一般第一个位置的代表 主内容 -->
            <FrameLayout
                android:id="@+id/main"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </FrameLayout>

            <!-- 左侧菜单(设置layout_gravity 为left) -->
            <RelativeLayout
                android:id="@+id/left"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="left">
            </RelativeLayout>

            <!-- 右侧菜单(设置layout_gravity 为right) -->
            <RelativeLayout
                android:id="@+id/right"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="right">
            </RelativeLayout>

        </android.support.v4.widget.DrawerLayout>
    </LinearLayout>

DrawerLayout一般分为三个部分 主内容,左侧菜单,右侧菜单
每个部分的内容自行设置,我是采用Fragment方式设置内容,这里仅供参考

    //新建Fragment,具体内容我就不详细说了
    fragmentMain = new FragmentMain();
    //添加内容,比较简单的
    getSupportFragmentManager().beginTransaction().replace(R.id.main, fragmentMain).commit();

到此为止已经初步实现了侧滑菜单的功能,来看一下效果

DrawerLayout初效果.gif

然后,就是给侧滑按钮添加效果了

1. 在此之前要进行view的初始化

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
    toolbar = (Toolbar) this.findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

2. 通过ActionBarDrawerToggle来完成效果,操作很简单

    mToggle = new ActionBarDrawerToggle(HomeActivity.this, 
                                        mDrawerLayout, 
                                        toolbar, 
                                        R.string.open,
                                          R.string.close);
    mToggle.syncState();
    mDrawerLayout.addDrawerListener(mToggle);

这样就结束了

最后就是解决DrawerLayout不能全屏滑动的问题
private void setDrawerLeftEdgeSize (Activity activity, DrawerLayout drawerLayout, float displayWidthPercentage) {
    if (activity == null || drawerLayout == null) return;
    try {
        // 找到 ViewDragHelper 并设置 Accessible 为true
        Field leftDraggerField =
                drawerLayout.getClass().getDeclaredField("mLeftDragger");//Right
        leftDraggerField.setAccessible(true);
        ViewDragHelper leftDragger = (ViewDragHelper) leftDraggerField.get(drawerLayout);

        // 找到 edgeSizeField 并设置 Accessible 为true
        Field edgeSizeField = leftDragger.getClass().getDeclaredField("mEdgeSize");
        edgeSizeField.setAccessible(true);
        int edgeSize = edgeSizeField.getInt(leftDragger);

        // 设置新的边缘大小
        Point displaySize = new Point();
        activity.getWindowManager().getDefaultDisplay().getSize(displaySize);
        edgeSizeField.setInt(leftDragger, Math.max(edgeSize, (int) (displaySize.x *
                displayWidthPercentage)));
    } catch (NoSuchFieldException e) {
    } catch (IllegalArgumentException e) {
    } catch (IllegalAccessException e) {
    }
}

直接调用这个方法即可!最后一个参数 传 1,即可实现全屏滑动。如果你想让右侧菜单也是全屏,只要将对应的 "mLeftDragger" 改为 "mRightDragger"。

我的博客地址
再次感谢那些给我提供帮助的文章,博客和人!!我会努力的!!

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
移動(dòng)開(kāi)發(fā)工程師
手記
粉絲
13
獲贊與收藏
84

關(guān)注作者,訂閱最新文章

閱讀免費(fèi)教程

  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消