Home > Java > javaTutorial > Android Basics: Use of Toolbar

Android Basics: Use of Toolbar

高洛峰
Release: 2016-11-15 10:02:13
Original
1215 people have browsed it

Toolbar was launched in Android 5.0 and is used to replace the ActionBar control. It can be highly customized and flexible to use. The official ToolBar must be used in systems above 5.0. If you need to use it in a lower version, you need to use support v7 Toolbar in the package.

The next step is the introduction of the Toolbar on the official website:

Toolbar 
extends ViewGroup 
java.lang.Object 
   ↳    android.view.View 
       ↳    android.view.ViewGroup 
           ↳    android.support.v7.widget.Toolbar
Copy after login

Android Basics: Use of Toolbar

Prerequisites for use

To use the ToolBar, you need to hide the ActionBar of the activity. So how to hide it? There are three methods. The following introduces each

Method 1:

Modify in the res/values/styles.xml file

<style name="AppTheme.Base" parent="Theme.AppCompat"> 
 <item name="windowActionBar">false</item> 
 <item name="android:windowNoTitle">true</item> 
</style>
Copy after login

You can also directly use the theme without ActionBar

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
Copy after login

Method 2:

Modify the theme of the activity in the manifest file, as shown below:

<activity android:name="MainActivity" 
           android:theme="@style/Theme.AppCompat.Light.NoActionBar"> 
</activity>
Copy after login

Method 3 :

Cancel directly in the code. Before setContentView.

requestWindowFeature(Window.FEATURE_NO_TITLE); 
       setContentView(R.layout.activity_main); 
       //supportRequestWindowFeature(Window.FEATURE_NO_TITLE);  在AppCompatActivity中
Copy after login

How to use

In the layout file. Add the following code. The position is not fixed.

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?android:attr/actionBarSize" 
    android:background="?attr/colorPrimaryDark" 
    app:title="Title" 
    app:titleTextColor="#222222" 
    app:logo="@mipmap/ic_launcher" 
    app:subtitle="subtitle"></android.support.v7.widget.Toolbar>
Copy after login

title is used to set the title. subtitle is used to set the subtitle. titleTextColor is used to set the title font color. background is used to set the background color. The effect is as follows:

Android Basics: Use of Toolbar

These values ​​can also be set dynamically in java code.

mToolbar.setTitle("JavaTitle"); 
       mToolbar.setSubtitle("JavaSubTitle"); 
       mToolbar.setLogo(R.mipmap.ic_launcher); 
       mToolbar.setNavigationIcon(android.R.drawable.ic_input_delete); 
       mToolbar.setOverflowIcon(ContextCompat.getDrawable(this, android.R.drawable.ic_menu_more)); 
/        setActionBar(mToolbar);  //activity中 
       setSupportActionBar(mToolbar);   //AppCompatActivity中
Copy after login

Add Menu

To add menu first we need to have menu, here I choose to create main.xml in res/menu/ to define the menu file. The code is as follows

<?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"> 
    <item 
        android:id="@+id/add" 
        android:icon="@android:drawable/ic_menu_add" 
        android:title="Add" 
        app:showAsAction="never|withText"/> 
    <item 
        android:id="@+id/delete" 
        android:icon="@android:drawable/ic_menu_delete" 
        android:title="Delete" 
        app:showAsAction="never|withText"/> 
    <item 
        android:id="@+id/edit" 
        android:icon="@android:drawable/ic_menu_edit" 
        android:title="Edit" 
        app:showAsAction="never|withText"/> 
    <item 
        android:id="@+id/email" 
        android:icon="@android:drawable/sym_action_email" 
        android:title="Email" 
        app:showAsAction="never|withText"/> 
 
</menu>
Copy after login

The following is the code in the java file.

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        getMenuInflater().inflate(R.menu.main, menu); 
        return true; 
    } 
 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
        switch (item.getItemId()) { 
            case android.R.id.home:  
                Toast.makeText(MainActivity.this, "您点击了NavigationIcon", Toast.LENGTH_SHORT).show(); 
                break; 
            case R.id.add: 
                Toast.makeText(MainActivity.this, "Add", Toast.LENGTH_SHORT).show(); 
                break; 
            case R.id.delete: 
                Toast.makeText(MainActivity.this, "Delete", Toast.LENGTH_SHORT).show(); 
                break; 
            case R.id.edit: 
                Toast.makeText(MainActivity.this, "Edit", Toast.LENGTH_SHORT).show(); 
                break; 
            case R.id.email: 
                Toast.makeText(MainActivity.this, "Email", Toast.LENGTH_SHORT).show(); 
                break; 
        } 
        return true; 
    }
Copy after login

Android Basics: Use of Toolbar

Show the Icon in the Menu

After writing this, we will find that the hidden menu does not display the icon, so how do we set it up:

Just need to re-do the method: The activity here is AppCompatActivity

@Override 
   protected boolean onPrepareOptionsPanel(View view, Menu menu) { 
       if (menu != null) { 
           if (menu.getClass() == MenuBuilder.class) { 
               try { 
                   Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE); 
                   m.setAccessible(true); 
                   m.invoke(menu, true); 
               } catch (Exception e) { 
                   Log.i("tag", "onPrepareOptionsPanel: " + 
                           getClass().getSimpleName() + 
                           "onMenuOpened...unable to set icons for overflow menu" 
                           + e); 
               } 
           } 
       } 
       return super.onPrepareOptionsPanel(view, menu); 
   }
Copy after login

The effect is as follows:

Android Basics: Use of Toolbar

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template