Android programming: using toolbar

Android programming: using toolbar


This article blog link:http://blog.csdn.net/jdh99 , author: jdh, reprint please specify.


surroundings:

Host: WIN10

Development environment: Android  Studio 2.2 Preview 3


Description:

Realize function:

  • Replace the three dots on the right side of the toolbar with a + sign
  • The pop-up menu option does not obscure the toolbar
  • Monitor the left drawer open

Effect picture:



Layout file:

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.bazhangkeji.classroom.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"
            app:popupTheme="@style/OverflowMenuStyle"
            app:layout_scrollFlags="scroll"/>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>


style definition:

<style name="OverflowMenuStyle" parent="@style/AppTheme.PopupOverlay">
        <item name="overlapAnchor">false</item>
    </style>

Note: If the overlapAnchor property is true, the pop-up menu will obscure the toolbar


Set the activity without ActionBar in AndroidManifest.xml:

<activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/AppTheme.NoActionBar" />


Source code:

private ActionBarDrawerToggle toggle;


Initialize the toolbar:

private void initToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("知行书院");
        setSupportActionBar(toolbar);
//        // 测试:toolbar点击
//        toolbar.setOnClickListener(l -> Logging.e("setOnClickListener"));
//        toolbar.setNavigationOnClickListener(v -> Logging.e("setNavigationOnClickListener"));

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(new MyDrawerListener());
        toggle.syncState();
    }

Note: If the drawer monitor class is defined, the toolbar.setNavigationOnClickListener(v -> Logging.e("setNavigationOnClickListener")); method will not take effect


Inherit DrawerListener to implement drawer status monitoring:

private class MyDrawerListener implements DrawerLayout.DrawerListener {
        @Override
        public void onDrawerOpened(View drawerView) {
            toggle.onDrawerOpened(drawerView);
            Log.e(TAG, "onDrawerOpened");
            getPersonalInfoTimestamp.start();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            toggle.onDrawerClosed(drawerView);
            Log.e(TAG, "onDrawerClosed");
        }

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            toggle.onDrawerSlide(drawerView, slideOffset);
        }

        @Override
        public void onDrawerStateChanged(int newState) {
            toggle.onDrawerStateChanged(newState);
        }
    }


Press the monitor menu key:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        
        switch (id) {
            case R.id.action_add_friend:
                Logging.e("action_add_friend");
                return true;
            case R.id.action_add_group:
                Logging.e("action_add_group");
                return true;
            case R.id.action_create_group:
                Logging.e("action_create_group");
                return true;
        }

        return super.onOptionsItemSelected(item);
    }


Reference link:

1. android: Detailed Explanation of ToolBar (Hand-in-hand tutorial)

2. How to use Toolbar and customize Toolbar

3. Android Material Design Toolbar and Palette Practice

4. Use PopMenu style of Android Toolbar (including overflow menu setting [Use of popup menu])

5. Combination of Android ActionBarDrawerToggle, DrawerLayout, ActionBar


Guess you like

Origin blog.csdn.net/jdh99/article/details/71402712