`

DrawerLayout的使用和背景设置去除阴影

阅读更多


drawerLayout是Support Library包中实现了侧滑菜单效果的控件,可以说drawerLayout是因为第三方控件如MenuDrawer等的出现之后,google借鉴而出现的产物。drawerLayout分为侧边菜单和主内容区两部分,侧边菜单可以根据手势展开与隐藏(drawerLayout自身特性)

用起来还是比较简单的:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/weather_drawer">

    <include layout="@layout/你的主布局layout"/>
    <!--主布局写在这里-->

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:id="@+id/weather_left_drawer"
        android:orientation="vertical"
        android:layout_gravity="left"
        android:background="@color/black">
        <ListView
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:id="@+id/left_drawer_listview"
            android:background="@color/black">
        </ListView>
    </LinearLayout>
    <!--左抽屉-->

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:id="@+id/weather_right_drawer"
        android:orientation="vertical"
        android:layout_gravity="right"
        android:background="@color/black">
        <ListView
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:id="@+id/right_drawer_listview"
            android:background="@color/black">
        </ListView>
    </LinearLayout>
    <!--右抽屉-->
</android.support.v4.widget.DrawerLayout>



主布局放在第一个,抽屉布局放在后面,android:layout_gravity="xxx",这个属性标识了drawer,这里需要吐槽一下,as的代码提示并没有这个属性。这个属性的值left或者right标识你的这个drawer是从左侧还是右侧划出来。
这时候有同学可能会问了,主布局放在后面行不行,放在中间行不行呀,反正drawerlayout都是靠layout_gravity识别layout_gravity的。
答案:不行。上面那个布局,若将主布局放在两个抽屉中间,会出现一个bug:左边抽屉划出来就划不回去了!(右边的没影响)诸君可自己尝试。


还有一个问题,默认状态下,抽屉划出来的时候会有一层阴影遮住主布局,如下图:


 

怎么解决这个问题呢,也还是蛮简单的,

drawerLayout.setScrimColor(Color.TRANSPARENT);

添加代码后效果如下图所示:


 

好了,drawerlayout的基本用法和一些小问题讲到这,想到啥我再补充吧。有问题请留言哟。
 
 

 

  • 大小: 67.5 KB
  • 大小: 91.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics