The use of Android side sliding menu bar DrawerLayout

android side sliding menu bar
Simple and crude side sliding menu bar
Insert image description here


## 二 xml R.layout.activity_style2页面


```xml
<?xml version="1.0" encoding="utf-8"?>
<!--侧边栏样式-->
<android.support.v4.widget.DrawerLayout
    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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <!--主页内容-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <!--标题栏样式一-->
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:background="@color/colorPrimary"
            app:subtitleTextColor="@android:color/white"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:title="样式2"
            app:titleTextColor="@android:color/white">
        </android.support.v7.widget.Toolbar>

        <!--主页内容-->
        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Hello World!"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>
        </android.support.constraint.ConstraintLayout>

    </LinearLayout>


    <!--侧边栏内容-->
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigationview_header"
        app:insetForeground="@android:color/transparent"
        app:menu="@menu/menu_navigation">

        <!--添加脚布局-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_gravity="bottom"
            android:gravity="center"
            android:orientation="horizontal">

            <Button
                android:id="@+id/footer_item_setting"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:background="@android:color/transparent"
                android:drawableStart="@drawable/ic_setting"
                android:drawableLeft="@drawable/ic_setting"
                android:drawablePadding="1dp"
                android:gravity="center"
                android:paddingLeft="5dp"
                android:text="设置"
                android:textAlignment="inherit"
                android:textColor="@color/colorPrimary"/>

            <Button
                android:id="@+id/footer_item_out"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@android:color/transparent"
                android:drawableStart="@drawable/ic_out"
                android:drawableLeft="@drawable/ic_out"
                android:drawablePadding="1dp"
                android:gravity="center"
                android:paddingLeft="5dp"
                android:text="退出"
                android:textAlignment="center"
                android:textColor="@color/colorPrimary"/>
        </LinearLayout>

    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
头像部分xml
<?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"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:background="@color/colorPrimary">

    <android.support.v7.widget.AppCompatImageView
        android:id="@+id/iv_head"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/ic_head"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:text="zhj"
        android:textColor="@android:color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/iv_head"/>

</android.support.constraint.ConstraintLayout>

选项xml

```xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <group
        android:id="@+id/group1"
        android:checkableBehavior="single">
        <item
            android:id="@+id/single_1"
            android:icon="@drawable/ic_android_fill"
            android:title="安卓"
            app:actionLayout="@layout/bage_hint"/>

        <item
            android:id="@+id/single_2"
            android:icon="@drawable/ic_apple_fill"
            android:title="苹果"
            app:actionLayout="@layout/vip_view"/>
    </group>

    <group
        android:id="@+id/group2"
        android:checkableBehavior="single">
        <item
            android:id="@+id/single_3"
            android:icon="@drawable/ic_github_line"
            android:title="github"/>

        <item
            android:id="@+id/single_4"
            android:icon="@drawable/ic_wechat_fill"
            android:title="wechat"/>
    </group>

    <item
        android:title="子菜单">
        <menu>
            <item
                android:id="@+id/item_2"
                android:icon="@drawable/ic_taobao_fill"
                android:title="淘宝"/>

            <item
                android:id="@+id/item_1"
                android:icon="@drawable/ic_baidu_line"
                android:title="百度"/>

            <item
                android:id="@+id/item_3"
                android:icon="@drawable/ic_qq_fill"
                android:title="腾讯"/>
        </menu>
    </item>
</menu>

The pictures in xml will not be placed

代码段
``public class Style2Activity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScreenInfoUtils.fullScreen(this);

        setContentView(R.layout.activity_style2);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        NavigationView navigationview = (NavigationView) findViewById(R.id.navigation_view);
        final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        setSupportActionBar(toolbar);//将toolbar与ActionBar关联
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, 0, 0);
        drawer.addDrawerListener(toggle);//初始化状态
        toggle.syncState();

        /*---------------------------添加头布局和尾布局-----------------------------*/
        //获取xml头布局view
        View headerView = navigationview.getHeaderView(0);
        //添加头布局的另外一种方式
        //View headview=navigationview.inflateHeaderView(R.layout.navigationview_header);

        //寻找头部里面的控件
        ImageView imageView = headerView.findViewById(R.id.iv_head);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "点击了头像", Toast.LENGTH_LONG).show();
            }
        });
        navigationview.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                return false;
            }
        });
        ColorStateList csl = (ColorStateList) getResources().getColorStateList(R.color.nav_menu_text_color);
        //设置item的条目颜色
        navigationview.setItemTextColor(csl);
        //去掉默认颜色显示原来颜色  设置为null显示本来图片的颜色
        navigationview.setItemIconTintList(csl);

        //设置消息数量
        LinearLayout llAndroid = (LinearLayout) navigationview.getMenu().findItem(R.id.single_1).getActionView();
        TextView msg = (TextView) llAndroid.findViewById(R.id.msg_bg);
        msg.setText("99+");

        //设置条目点击监听
        navigationview.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                //点击哪个按钮
                Toast.makeText(getApplicationContext(), menuItem.getTitle(), Toast.LENGTH_LONG).show();
                //设置哪个按钮被选中
//                menuItem.setChecked(true);
                //关闭侧边栏
//                drawer.closeDrawers();
                return false;
            }
        });

        /*---------------------------自定义侧边栏布局-----------------------------*/
//        getSupportFragmentManager().beginTransaction().replace(R.id.navigation_view,
//                new NavigationViewFragment()).commit();
    }
}

Guess you like

Origin blog.csdn.net/weixin_47062100/article/details/122962198