Toolbar easy to use

Keywords: Android

Toolbar

This paper mainly introduces the use of toolbar. Under the v7 package, toolbar is a control that doesn't talk much and directly explains the steps.

  • First, the theme (style) should inherit NoActionBar


    <style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">

        <item name="colorPrimary">@color/colorPrimary</item>

        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>

        <item name="colorAccent">@color/colorAccent</item>

    </style>
  • Introducing toolbars in layout files


    <android.support.v7.widget.Toolbar

            android:id="@+id/toolbar"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"/>

toolbar can set title and subtitle

  • Set setSupportActionBar(toolbar) in Activity;

So the toolbar is displayed.

Set icon on left

app:navigationIcon="@mipmap/ic_launcher_round"

Set logo

app:logo="@android:drawable/ic_lock_power_off"

Notice the difference between the navtgation and logo in the figure above

The text of the toolbar title can be changed according to the titleTextAppearance

 app:titleTextAppearance="@style/ToolbarTextStyle"





<!--Set up toolbar Text style for title-->

<style name="ToolbarTextStyle" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">

    <item name="android:textSize">13sp</item>

</style>

You can also use the built-in return arrow. At this time, you need to remove navigationIcon. Otherwise, the built-in return button cannot be displayed.



        setSupportActionBar(toolbar);



        ActionBar actionBar = getSupportActionBar();

        if (null != actionBar) {

            actionBar.setHomeButtonEnabled(true);

            actionBar.setDisplayHomeAsUpEnabled(true);

        }

Note that getSupportActionBar needs to be called after setSupportActionBar

Similarly, click the left button to listen to the event



    toolbar.setNavigationOnClickListener(new View.OnClickListener() {

                @Override

                public void onClick(View v) {

                    Toast.makeText(MainActivity.this, "Return", Toast.LENGTH_SHORT).show();

                }

            });

There is also a way to return activity:

Configuration in the manifest file: configuration in the activity of the target

<activity

            android:name=".SecondActivity"

            android:parentActivityName=".MainActivity"

            >

        //Compatible with versions below 4.0

            <meta-data

                android:name="android.support.PARENT_ACTIVITY"

                android:value=".MainActivity"

                />

        </activity>
  • Set menu for toolbar

    Override onCreateOptionsMenu in activity



    @Override

        public boolean onCreateOptionsMenu(Menu menu) {

            getMenuInflater().inflate(R.menu.menu_main, menu);

            return true;

        }

Click event of menu needs to be rewritten

     @Override

        public boolean onOptionsItemSelected(MenuItem item) {

            return true;

        }

Posted by Adika on Sun, 05 Apr 2020 08:57:07 -0700