Android--ToolBar使用

作者: 芒果味的你呀 | 来源:发表于2017-08-28 21:20 被阅读181次

官方文档对Toolbar的介绍,可以知道Toolbar主要包括五部分:

image.png

1 导航按钮
2 应用Logo
3 标题与副标题
4 若干自定义View
5 ActionMenu

Toolbar常用的方法

在抽屉布局中 我们经常会看左边1的导航图标的监听事件是(打开关闭抽屉)
        Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        DrawerLayout drawer= (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
有的时候1是黑色的,此时我们要在toolbal样式xml中
  app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
当弹出字体为黑色的
  app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
不用设置1的图标 系统默认的就是1图标
设置导航图标及点击事件:
image.png

有的时候我们要重写这个图标 并为其监听事件

toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
//
     app:navigationIcon="@drawable/ic_arrow_back_black_24dp"
     android:navigationIcon="@drawable/ic_arrow_back_black_24dp"

主题设置
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"(toolbar上的颜色为白色,黑色突兀) 
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"(弹出字体为浅色主题)
xml中其他常用配置
 <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_normal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:title="主标题"
        app:titleTextColor="#ffffff"
        android:background="@color/colorPrimaryDark"
        app:subtitle="子标题"
        app:subtitleTextColor="#ffffff"
        app:logo="@mipmap/ic_launcher"
  
>
menu菜单设置

首先在menu/下建立相应的menu文件 toolber.xml

<?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">
<!--    android:icon="@drawable/ic_favorite_border_black_24dp"-->
    <item
        android:id="@+id/collect"
        android:title="Collect"
        android:icon="@drawable/ic_favorite"
        app:showAsAction="always">
    </item>
    <item
        android:id="@+id/share"
        android:icon="@drawable/ic_share"
        android:title="Share"
        app:showAsAction="always">
    </item>
</menu>
重写这三个方法:
 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.collect:
                Toast.makeText(this, "收藏成功", Toast.LENGTH_LONG).show();
                break;
            case R.id.share:
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_SUBJECT, "哈哈哈");
                intent.putExtra(Intent.EXTRA_TEXT, "标题:" + "www.baidu.com" + "\n" + "---test");
                //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(Intent.createChooser(intent, "ttttt"));
                break;
            default:
        }
        return super.onOptionsItemSelected(item);
    }

其他

//默认会显示应用名,将其隐藏
       getSupportActionBar().setDisplayShowTitleEnabled(false);

相关文章

网友评论

    本文标题:Android--ToolBar使用

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