美文网首页
Android studio:toolbar 自定义及右侧滑动

Android studio:toolbar 自定义及右侧滑动

作者: xiaoli9527 | 来源:发表于2017-08-01 01:35 被阅读0次

    无言

    1、build.gradle 导入 compile'com.android.support:design:25.3.1'
    2、添加自定义头部导航栏app_bar_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 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"
        android:fitsSystemWindows="true"
        tools:context="com.maibo.tiziyouxi.MainActivity">
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:focusable="true"
                android:focusableInTouchMode="true"
                app:popupTheme="@style/AppTheme.AppBarOverlay">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_gravity="center"
                    android:text="@string/app_name"
                    android:textColor="@android:color/white"
                    android:textSize="20sp"
                    android:textStyle="bold" />
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.AppBarLayout>
        <include layout="@layout/app_content" />
    </android.support.design.widget.CoordinatorLayout>
    

    2、添加中间主题内容app_content.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"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/content_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.maibo.tiziyouxi.NavigationBarActivity"
        tools:showIn="@layout/home_toolbar">
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="0dp"
            android:background="#e8e8e8">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                </TableLayout>
        </ScrollView>
    </RelativeLayout>
    

    3、添加侧拉菜单头部 nav_header_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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="@dimen/nav_header_height"
        android:background="@drawable/side_nav_bar"
        android:gravity="bottom"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:theme="@style/ThemeOverlay.AppCompat.Dark">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/nav_header_vertical_spacing"
            android:text="用户名:test"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textColor="#ffffff" />
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="余额:0.0 元"
            android:textColor="#ffffff" />
    </LinearLayout>
    

    4、在acticity_main.xml 中添加自定义的toolbar 跟 content

        <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="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer" />
    

    5、在MainActivity.java 中开始创建侧拉菜单

    public DrawerLayout drawer;
    protected void onCreate(Bundle savedInstanceState) {
            menuInit();// 初始化菜单
    }
    public void menuInit(){
            // toolbar
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            toolbar.setTitle("");
            setSupportActionBar(toolbar);
    
            //初始化侧滑菜单
            drawer = (DrawerLayout) findViewById(R.id.activity_main);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.setDrawerListener(toggle);
    //        toggle.syncState(); // 不设置toolbar的navigation按钮
    
            // 点击右侧按钮显示菜单
            toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    if (!drawer.isDrawerOpen(Gravity.RIGHT)) {
                        drawer.openDrawer(Gravity.RIGHT);
                    }
                    return false;
                }
            });
    
            // 选中隐藏右侧菜单
            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem item) {
                    // 子菜单点击隐藏菜单
                    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.activity_main);
                    drawer.closeDrawer(Gravity.RIGHT);
                    // 子菜单点击触发事件
                    int id = item.getItemId();
                    if (id == R.id.nav_kjjg) {
                    } 
                    return true;
                }
            });
        }
     // 点击背景隐藏菜单
        @Override
        public void onBackPressed() {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.activity_main);
            if (drawer.isDrawerOpen(Gravity.RIGHT)) {
                drawer.closeDrawer(Gravity.RIGHT);// 从右侧回收
            } else {
                super.onBackPressed();
            }
        }
        // 添加菜单按钮
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    

    相关文章

      网友评论

          本文标题:Android studio:toolbar 自定义及右侧滑动

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