Better than Tollbar ActionBar with

Better than Tollbar ActionBar with

ActionBar

  • ActionBar title bar control for each activity.
  • Because of design issues ActionBar is limited, only the top of the activity

Tollbar

  • Tollbar is a control Material of.
  • Nice place Tollbar because, Tollbar inherited from ActionBar, improve their flexibility foundation has all the features of the ActionBar, in conjunction with other controls to complete some Material Design [^ 1] effect

Use Tollbar

  • The new project is a default ActionBar

Let Tollbar visual effect is more obvious

Modify the res / values ​​/ styles.xml APPTheme in a name for the style, theme designated parent pastel theme, there are usually two values

Theme.AppCompat.NoActionBar(Dark theme) and Theme.AppCompat.Light.NoActionBar(pale themes)

Tollbar replace ActionBar

In activity_main.xml file to add appcomat-v7 library Tollbar controls

<androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"<!--高度设置为ActionBar的高度-->
                android:background="?attr/colorPrimary"//<!--背景色设置为colorPrimary-->
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"<!--Tollbar主题-->
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/><!--弹出来的菜单项淡色主题-->

Modify MainActivity.java file

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);//获取Toolbar的实例
        setSupportActionBar(toolbar);//调用方法传入toolbar
    }

At this point Tollbar and ActionBar on the same.

Change the title name on the Tollbar

Edit the title name, add android activity tags AndroidManifest.xml file: label = "title name."

Tollbar to add some action buttons

  1. Ready to add a few vector (recommended Alibaba vector library download) stored in the drawable directory.
  2. Create a new menu folder directory in res, right-click menu folder a new Menu resource file
  3. Tollbar.xml create a file, add the code
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/info"
        android:icon="@drawable/info"
        android:title="Info"
        app:showAsAction="always"/><!--always表示永远显示在Toolbar中,屏幕不够不显示-->
    <item
        android:id="@+id/delete"
        android:icon="@drawable/delete"
        android:title="Delete"
        app:showAsAction="ifRoom"/><!--ifroom表示屏幕空间足够的话显示,不够则显示在菜单中,
                                        never表示永远显示在菜单中-->
</menu>
  1. MainActivity.java add Click event code, I will just have to add the test button code is not used, the specific logic can be modified depending on the circumstances.
   @Override
       public boolean onOptionsItemSelected(@NonNull MenuItem item) {
           switch (item.getItemId()){
               case R.id.info:
                   Toast.makeText(this,"info",Toast.LENGTH_SHORT).show();
                   break;
               case R.id.delete:
                   Toast.makeText(this,"delete",Toast.LENGTH_SHORT).show();
                   break;
                   default:
           }
           return true;
       }

Well, this code is written, you can take a look at the run, on the first of the basic functions Toolbar Having said that, the welcome of all the great God pointing.

[^ 1]: Material Design is a set of design principles Google interface design

Guess you like

Origin www.cnblogs.com/good-good-study-cl/p/12079727.html