Android Toolbar使用(二)

作者: 年华_零落成诗 | 来源:发表于2017-07-03 01:59 被阅读198次

新手一枚,请多指教🙏🏻🙏🏻🙏🏻

(一)Toolbar 加载menu

创建Toolbar

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.Toolbar>

获取组件并加载菜单

           private void initView() {
        //获取组件
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        //设置主标题
        mToolbar.setTitle("这是主标题");
        //设置副标题
        mToolbar.setSubtitle("这是副标题");
        //设置主标题颜色
        mToolbar.setTitleTextColor(Color.WHITE);
        //设置副标题颜色
        mToolbar.setSubtitleTextColor(Color.WHITE);
        //加载菜单
        mToolbar.inflateMenu(R.menu.menu);
        //点击事件
        mToolbar.setOnMenuItemClickListener(this);
        //overflower图标
        mToolbar.setOverflowIcon(getDrawable(R.drawable.ic_more));

    }


创建一个menu文件

menu

设置几个item

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
    app:showAsAction="always"
    android:title="收藏"
    android:icon="@drawable/ic_bookmark"
    android:id="@+id/collect"
        />
    <item
        android:title="删除"
        android:icon="@drawable/ic_delete"
        android:id="@+id/delete"
        />

运行效果

运行效果

点击事件

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.collect:
                showToast("收藏");
                break;
            case R.id.delete:
                showToast("删除");
                break;
        }
        return false;
    }

    /**
     * Toast 方法
     * @param s
     */
    private void showToast(String s) {
        Snackbar.make(mToolbar,s,Snackbar.LENGTH_SHORT).show();
    }

运行效果

运行效果

(二)ActionMenuView 使用

创建 ActionMenuView

     <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.ActionMenuView
            android:id="@+id/action_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </android.support.v7.widget.Toolbar>

获取组件

        private void initView() {
        //获取组件
        mActionView = (ActionMenuView) findViewById(R.id.action_view);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        //设置点击事件
        mActionView.setOnMenuItemClickListener(this);
        //设置成为 actinobar
        setSupportActionBar(mToolbar);
        //不显示标题
        getSupportActionBar().setDisplayShowTitleEnabled(false);
    }

创建一个menu文件

menu

设置几个item

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
    app:showAsAction="always"
    android:title="收藏"
    android:icon="@drawable/ic_bookmark"
    android:id="@+id/collect"
        />
    <item
        app:showAsAction="always"
        android:title="删除"
        android:icon="@drawable/ic_delete"
        android:id="@+id/delete"
        />
    <item
        android:id="@+id/help"
        app:showAsAction="always"
        android:title="帮助"
        android:icon="@drawable/ic_live_help"
        />
    <item
        android:id="@+id/translate"
        app:showAsAction="always"
        android:title="翻译"
        android:icon="@drawable/ic_translate"
        />
    <item
        android:id="@+id/bluetooth"
        app:showAsAction="always"
        android:title="蓝牙"
        android:icon="@drawable/ic_bluetooth"
        />
</menu>

设置menu

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,mActionView.getMenu());
        return super.onCreateOptionsMenu(menu);
    }

运行效果:

效果

点击事件:

//实现此方法
 @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.collect:
                showToast("收藏");
                break;
            case R.id.delete:
                showToast("删除");
                break;
            case R.id.help:
                showToast("帮助");
                break;
            case R.id.translate:
                showToast("翻译");
                break;
            case R.id.bluetooth:
                showToast("蓝牙");
                break;
        }
        return false;
    }
    //Toast
    private void showToast(String s) {
        Snackbar.make(mToolbar,s,Snackbar.LENGTH_SHORT).show();
    }

点击效果:

点击

ps:一点小技巧

      //这样使用可以设置overflower图标
        mToolbar.inflateMenu(R.menu.menu);
        mToolbar.setOverflowIcon(getDrawable(R.drawable.ic_more));

相关文章

网友评论

    本文标题:Android Toolbar使用(二)

    本文链接:https://www.haomeiwen.com/subject/qvgncxtx.html