Basic usage of Android ToolBar

What is ToolBar

ToolBar is a navigation control launched by Android in 5.0, which is used to replace the previous ActionBar. ToolBar is much more flexible than ActionBar. Simply put, ToolBar can be used as a new control. And ToolBar is more customizable.

Using ToolBar

For backward compatibility, ToolBar from the compatible library is usually used. After adding the compatible library, you can start using ToolBar. First set the theme of NoActionBar in XML. Then add ToolBar in the layout code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:fitsSystemWindows="true"
    tools:context="com.swpuiot.secondarytradingplatformv20.ToolBarActivity">

    <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/AppTheme.PopupOverlay" />

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

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

</LinearLayout>

At this time, you can see the navigation bar when you start the activity. This navigation bar is a ToolBar instead of the previous ActionBar. Then find the ToolBar control in the Activity, and you can perform many operations.


public class ToolBarActivity extends AppCompatActivity {
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tool_bar);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);//用toolbar取代以前的actionBar
        toolbar.setTitle("我是标题");
        toolbar.setNavigationIcon(R.mipmap.ic_launcher);//导航图标
        toolbar.setSubtitle("我是小标题");
        toolbar.setLogo(R.mipmap.ic_launcher);//Logo
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(ToolBarActivity.this, "You Click NavigationIcon", Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //这个方法是用来设置点击事件的
        switch (item.getItemId()) {
            case R.id.menu_clock:
                Toast.makeText(ToolBarActivity.this, "You Click clock", Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu_listen:
                Toast.makeText(ToolBarActivity.this, "You Click Listen", Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //系统会调用这个方法来初始化菜单
    getMenuInflater().inflate(R.menu.menu_test,menu);
    return true;
}


}

When adding the menu button, you need to create a resource file under res/menu

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".ToolBarActivity">
    <item
        android:id="@+id/menu_clock"
        android:title="clock"
        android:icon="@drawable/menu_clock"
        app:showAsAction="ifRoom|withText"
        />
    <item
        android:id="@+id/menu_listen"
        android:title="listen"
        android:icon="@drawable/ic_action_name"
        app:showAsAction="ifRoom|withText"
        />
</menu>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325640581&siteId=291194637