compile 'com.android.support:appcompat-v7:24.0.0'

自定义颜色

enter image description here

自定义toolbar上的控件

enter image description here

java代码示例:

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.inflateMenu(R.menu.menu_main);
    toolbar.setLogo(R.mipmap.ic_launcher);
    toolbar.setTitle("My Title");
    toolbar.setSubtitle("Sub title");
    toolbar.setNavigationIcon(R.mipmap.ab_android);
    toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {

            String msg = "";
            switch (item.getItemId()) {
                case R.id.action_edit:
                    msg += "Click edit";
                    break;
                case R.id.action_share:
                    msg += "Click share";
                    break;
                case R.id.action_settings:
                    msg += "Click setting";
                    break;
            }

            if(!msg.equals("")) {
                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    });

xml代码示例:

 <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:title="测试标题"/>

menu_main.xml

<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=".SwipeRefreshActivity" >

    <item android:id="@+id/action_edit"
        android:title="@string/action_edit"
        android:orderInCategory="80"
        android:icon="@mipmap/ab_edit"
        app:showAsAction="ifRoom" />

    <item android:id="@+id/action_share"
        android:title="@string/action_edit"
        android:orderInCategory="90"
        android:icon="@mipmap/ab_share"
        app:showAsAction="ifRoom" />

    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never"/>

    <item
        android:id="@+id/action_about"
        android:orderInCategory="101"
        android:title="关于"
        app:showAsAction="never" />
</menu>

showAsAction的意思:

* always:总是显示在界面上

* never:不显示在界面上,只让出现在右边的三个点中

* ifRoom:如果有位置才显示,不然就出现在右边的三个点中

参考文章

标签: none

评论已关闭