Android中Toolbar的使用

今天介绍的toolbar是为了取代过去的actionbar的,在material design中有一个统一的名字就是app bar,是Android5.0出现的,推出兼容版的 Toolbar 。为此,我们需要在工程中引入 appcompat-v7 的兼容包,使用 android.support.v7.widget.Toolbar 进行开发。

我们来翻译一下Toolbar的官方文档:

android.support.v7.widget
public class Toolbar
extends ViewGroup
A standard toolbar for use within application content.
A Toolbar is a generalization of action bars for use within application layouts. While an action bar is traditionally part of an Activity's opaque window decor controlled by the framework, a Toolbar may be placed at any arbitrary level of nesting within a view hierarchy. An application may choose to designate a Toolbar as the action bar for an Activity using the setSupportActionBar() method.
Toolbar supports a more focused feature set than ActionBar. From start to end, a toolbar may contain a combination of the following optional elements:
A navigation button. This may be an Up arrow, navigation menu toggle, close, collapse, done or another glyph of the app's choosing. This button should always be used to access other navigational destinations within the container of the Toolbar and its signified content or otherwise leave the current context signified by the Toolbar. The navigation button is vertically aligned within the Toolbar's minimum height, if set.
A branded logo image. This may extend to the height of the bar and can be arbitrarily wide.
A title and subtitle. The title should be a signpost for the Toolbar's current position in the navigation hierarchy and the content contained there. The subtitle, if present should indicate any extended information about the current content. If an app uses a logo image it should strongly consider omitting a title and subtitle.
One or more custom views. The application may add arbitrary child views to the Toolbar. They will appear at this position within the layout. If a child view's Toolbar.LayoutParams indicates a Gravity value of CENTER_HORIZONTAL the view will attempt to center within the available space remaining in the Toolbar after all other elements have been measured.
An action menu. The menu of actions will pin to the end of the Toolbar offering a few frequent, important or typical actions along with an optional overflow menu for additional actions. Action buttons are vertically aligned within the Toolbar's minimum height, if set.
In modern Android UIs developers should lean more on a visually distinct color scheme for toolbars than on their application icon. The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer.

大致意思:
  Toolbar是android.support.v7.widget下的,是公共类工具栏,集成至ViewGroup,
在应用程序中使用的标准工具栏的内容。
工具栏是在应用程序中使用的泛化使用的布局。action bar传统上是不透明的窗口由framework控制,toolbar可以放置在任意嵌套在一个视图层次的水平。应用程序可以选择指定的toolbar作为action使用setSupportActionBar()方法。
toolbar支持比Action Bar更多的特性,从始至终,一个toolbar可能包含以下的组合元素:
1.一个导航栏的按钮:这可能是一个箭头,导航菜单切换,关闭,折叠,完成应用程序的另一个字形的选择。这个按钮总是使用导航目的地在toolbar中,并且它工具栏和其所指内容或者许可当前上下文所指的toolbar。这个导航按钮是垂直对齐在toolbar的最小高度中,如果设置的话。
2.一个图标:这个扩展高度到bar,并且到任意的宽度。
3.一个标题和副标题:一个标题应该是一个标志toolbar的当前位置在导航层次结构中和所包含的内容。副标题,如果现在应该显示任何扩展当前的信息内容。如果一个应用程序使用图标,应该强烈考虑省略标题和副标题。
4.一个或更多的自定义的View:应用程序可以将任意子视图添加到toolbar中。它们将出现在这个布局中的位置。如果一个子视图的Toolbar.LayoutParams表示Gravity(对齐的方式)的值,是CENTER_HORIZONTAL(水平居中),在其他元素获取到空间后,视图将尝试获取toolbar的剩余空间。
5.一个操作的菜单:菜单的活动绑定到toolbar结束,提供几个的频繁的,重要的或是典型的行为,连同一个充满的菜单附加行为。这个操作按钮是垂直对齐在toolbar的最小高度中,如果设置的话。
在现代Android ui开发人员应该更多的依靠toolbar的视觉独特的配色方案比在它的应用程序图标。使用应用程序图标+标题作为标准,是API 21更新设备。

我们来学习toolbar简单使用:

添加依赖compile ‘com.android.support:appcompat-v7:23.2.1’
若想修改style的风格,可以在res下values下style.xml中修改
主题的样式:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

MainActivity(第一种写法):

package com.example.mac.coordinatelayoutdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

/**
 * Created by mac on 16-7-26.
 */
public class C extends AppCompatActivity {

    private Toolbar toolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_toolbar);
        toolbar = (Toolbar) findViewById(R.id.my_toolbar);
        //设置app logo
        toolbar.setLogo(R.mipmap.ic_launcher);
        //设置标题
        toolbar.setTitle("标题");
        //设置副标题
        toolbar.setSubtitle("副标题");
        //setNavigationIcon 设置导航栏的按钮,但是要设定在setSupoortActionBar才有作用,否则会出现 back button
        /toolbar.setNavigationIcon(R.mipmap.ic_launcher);
        //隐藏系统默认的Title
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);

        //设置右上角的填充菜单
        toolbar.inflateMenu(R.menu.my_menu);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                int menuItemId = item.getItemId();
                if (menuItemId == R.id.action_settings) {
                    Toast.makeText(C.this, "aaa", Toast.LENGTH_SHORT).show();
                } else if (menuItemId == R.id.holle) {
                    Toast.makeText(C.this, "bbb", Toast.LENGTH_SHORT).show();
                } else if (menuItemId == R.id.date) {
                    Toast.makeText(C.this, "ccc", Toast.LENGTH_SHORT).show();
                }

                return true;
            }
        });
    }
}

MainActivity中(第二种写法):


package com.example.mac.coordinatelayoutdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

/**
 * Created by mac on 16-7-26.
 */
public class C extends AppCompatActivity {

    private Toolbar toolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_toolbar);
        toolbar = (Toolbar) findViewById(R.id.my_toolbar);
        //设置app logo
        toolbar.setLogo(R.mipmap.ic_launcher);
        //设置标题
        toolbar.setTitle("标题");
        //设置副标题
        toolbar.setSubtitle("副标题");
        //设置为activity的App Bar
        setSupportActionBar(toolbar);
//设置返回键可用
getSupportActionBar().setHomeButtonEnabled(true);
//设置返回键显示
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //setNavigationIcon 设置导航栏的按钮,但是要设定在setSupoortActionBar才有作用,否则会出现 back button
        toolbar.setNavigationIcon(R.mipmap.ic_launcher);
        //隐藏系统默认的Title
      getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.my_menu, menu);
        return true;
    }


//重写onOptionsItemSelected()监听事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.holle) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

MainActivity的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

menu文件(在res下建的menu文件,在menu文件下建my_menu.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">

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="测试"
        app:showAsAction="never" />
    <item
        android:id="@+id/holle"
        android:title="你好"
        app:showAsAction="never" />
    <item
        android:id="@+id/date"
        android:icon="@mipmap/ic_launcher"
        android:orderInCategory="90"
        android:title="选择日期"
        app:showAsAction="ifRoom" />
</menu>

本人菜鸟一个有什么不对的地方希望大家指出评论,大神勿喷,希望大家一起努力学习!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值