美文网首页
Drawlayout的使用

Drawlayout的使用

作者: 皓皓amous | 来源:发表于2019-07-25 14:19 被阅读0次

1.布局代码

image.png
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

 <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

<android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
           />
    </android.support.v4.widget.DrawerLayout>


    <!--这里是主视图-->
    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</android.support.constraint.ConstraintLayout>...

2.布局二

image.png
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

        <!--这里是主视图-->
        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />


        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            android:background="@android:color/holo_green_light"
           />
    </android.support.v4.widget.DrawerLayout>

</android.support.constraint.ConstraintLayout>

3.mainActivity中的代码:

  private void initDrawerLayout() {
        //向系统设置toolbar
        setSupportActionBar(toolbar);
        //设置线性管理器
        LinearLayoutManager manager = new LinearLayoutManager(this);
        //ActionBarDrawerToggle  是 DrawerLayout.DrawerListener实现,改变android.R.id.home返回图标。
        //.Drawer拉出、隐藏,带有android.R.id.home动画效果。.监听Drawer拉出、隐藏;
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, 
      R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        // drawer.setDrawerListener(toggle);
        //上面的监听被下面的就监听给取代了,由于回报空指针异常,google推出了addDrawerListener(),
        // 对它做了空指针的判断
        drawerLayout.addDrawerListener(toggle);
        //该方法会自动和actionBar关联, 将开关的图片显示在了action上,如果不设置,也可以有抽屉的效果,不过是默认的图标
        toggle.syncState();
    }

相关文章

网友评论

      本文标题:Drawlayout的使用

      本文链接:https://www.haomeiwen.com/subject/onchrctx.html