ToolBar

1、设置style

<style name="AppTheme" parent="AppTheme.Base">
        <!-- Customize your theme here. -->
        <item name="android:navigationBarColor">@color/colorPrimary</item>
    </style>

    <!--为方便更改-->
    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <!--ActionBar的颜色,所以ToolBar用不到-->
        <item name="colorPrimary">@color/colorPrimary</item>
        <!--状态bar的颜色-->
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <!--未知-->
        <item name="colorAccent">@color/colorAccent</item>

        <!--窗口背景颜色-->
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:searchViewStyle">@style/MySearchView</item>
    </style>

    <style name="MySearchView" parent="Widget.AppCompat.SearchView"/>

2、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=".MainActivity">

    <!--id用于代码中的geiItemId-->
    <!--若无icon时,则view显示的是title-->
    <!--orderInCategory的大小决定同一个showAsAction里的排序-->
    <!--ifRoom无弹出窗口,never有类似于popmenu的效果-->
    <item android:id="@+id/action_search"
        android:title="search"
        android:orderInCategory="80"
        android:icon="@drawable/search"
        app:showAsAction="ifRoom" />

    <item android:id="@+id/action_share"
        android:title="share"
        android:orderInCategory="90"
        android:icon="@drawable/share"
        app:showAsAction="ifRoom" />

    <item android:id="@+id/action_more"
        android:title="more"
        android:orderInCategory="100"
        android:icon="@drawable/more"
        app:showAsAction="never"/>
</menu>

3、layout布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="lai.com.toolbar.MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimaryDark">

    </android.support.v7.widget.Toolbar>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/toolbar"
        android:text="Hello World!" />
</RelativeLayout>

4、Java代码

private Toolbar.OnMenuItemClickListener onMenuItemClickListener = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            String msg = "";
            switch (item.getItemId()) {
                case R.id.action_search:
                    msg += "Click search";
                    break;
                case R.id.action_share:
                    msg += "Click share";
                    break;
                case R.id.action_more:
                    msg += "Click more";
                    break;
            }

            if(!msg.equals("")) {
                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        toolbar.setLogo(R.mipmap.ic_launcher);
        toolbar.setTitle("主标题");
        toolbar.setSubtitle("副标题");
        setSupportActionBar(toolbar);

        toolbar.setOnMenuItemClickListener(onMenuItemClickListener);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_toolbar, menu);
        return true;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值