首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >001android初级篇之ToolBar

001android初级篇之ToolBar

作者头像
上善若水.夏
发布2018-09-28 10:33:08
4290
发布2018-09-28 10:33:08
举报
文章被收录于专栏:上善若水上善若水

官方的最新support library v7中提供了新的组件ToolBar,用来替代之前的ActionBar,实现更为弹性的设计在 material design 也对之做了名称的定义:App bar。下面描述下它的基本用法,权作抛砖引玉。

基本用法

如下代码,实现了主副标题及Logo和标题颜色的设置

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("交易清单");
toolbar.setSubtitle("--2015年度明细");
toolbar.setLogo(R.drawable.icon);
toolbar.setTitleTextColor(Color.parseColor("#FFFFFF"));
setSupportActionBar(toolbar);

设置菜单按钮额操作

需要首先编辑xml布局文件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=".MainActivity">
    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />
    <item android:id="@+id/action_share" android:title="share"
        android:orderInCategory="100" app:showAsAction="ifRoom" />
</menu>

其中 app:showAsAction有三个可选的值

always:总是显示在界面上
never:不显示在界面上,只让出现在右边的三个点中
ifRoom:如果有位置才显示,不然就出现在右边的三个点中

android:orderInCategory

表明摆放的顺序,不一定从0还是计算,但必须大于等于0,数值小的位于前,如果数值一样,在我们这个例子中3又两个值,则安顺序摆放;

代码处理

设置布局文件
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(R.menu.menu_main, menu);
 return true;
 }
 设置键值处理
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        mTextView.setText("Your Press Setting!\n");
        return true;
    }
    else if(id == R.id.action_share){
        mTextView.setText("share!");
        return true;
    }

    return super.onOptionsItemSelected(item);
}

去除ActionBar

去除Actionbar最简单的方法就是直接继承NoActionBar的主题了

参考链接

  1. android:ToolBar详解
  2. Android Material Design之Toolbar与Palette实践
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015.11.06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 基本用法
  • 设置菜单按钮额操作
    • 需要首先编辑xml布局文件menu_main.xml
      • 代码处理
      • 去除ActionBar
      • 参考链接
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档