Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Toolbar in Android

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use Toolbar in Android". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use Toolbar in Android.

Toolbar is a control introduced by Android5.0 to replace ActionBar. Can be highly customized, flexible to use. The official ToolBar must be used on a system above 5.0, if needed in an earlier version. You need to use Toolbar in the support v7 package.

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

Use premise

To use ToolBar, you need to hide the ActionBar of activity. So how to hide it? There are three ways. The following are described separately.

Method 1:

Modify in the res/values/styles.xml file

False true

You can also use themes without ActionBar directly.

Method 2:

Modify the theme that makes the activity in the manifest file, as follows:

Method 3:

Cancel directly in the code. Before setContentView.

RequestWindowFeature (Window.FEATURE_NO_TITLE); setContentView (R.layout.activity_main); / / supportRequestWindowFeature (Window.FEATURE_NO_TITLE); in AppCompatActivity

How to use it

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

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:

These values can also be set dynamically in the 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); / / setSupportActionBar (mToolbar) in activity; / / AppCompatActivity

Add Menu

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

Here 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, "you clicked 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;}

Show Icon in Menu

After writing here. We will find out. Hidden menu does not show icon, so how do we set it:

Just need to redo 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);} Thank you for reading, the above is the content of "how to use Toolbar in Android". After the study of this article, I believe you have a deeper understanding of how to use Toolbar in Android, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report