Android5.0 之 ToolBar基礎功能

Android5.0 之 ToolBar基礎功能

ToolBar是android 在 support library 裏面提供的用來取代 actionbar 的控件,而現在於 material design 中有一個統一名稱:app bar。下面將對Toolbar使用做一些簡單的介紹。

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle(R.string.app_name);
    //設置ToolBar字體顏色        
        toolbar.setTitleTextColor(Color.parseColor(getResources().getString(R.string.action_bar_title_color)));
        //設置中間的Logo
        toolbar.setLogo(R.drawable.ic_launcher);
        toolbar.collapseActionView();
        toolbar.setBackground(getResources().getDrawable(R.drawable.skin2));
        //支持ToolBar
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
 //設置指示圖片        getSupportActionBar().setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        toolbar.setNavigationOnClickListener()

----------


    <style name="Base.Theme.DesignDemo" parent="Theme.AppCompat.Light.NoActionBar">
        //ToolBar背景設置
        <item name="colorPrimary">#673AB7</item>
        //屏幕最上方電量欄背景設置
        <item name="colorPrimaryDark">#512DA8</item>
        //佈局中控件顯示的默認顏色
        <item name="colorAccent">#FF4081</item>
        <item name="android:windowBackground">@color/window_background</item>
    </style>

//ToolBar在xml文件使用
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ToolbarTheme"
        //app:popupTheme,這個屬性就是用來自定義我們彈出的菜單的樣式
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

Theme 屬性對應圖

設置ToolBar右側菜單

toolbar.setOnMenuItemClickListener(onMenuItemClick);
private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
  @Override
  public boolean onMenuItemClick(MenuItem menuItem) {
    String msg = "";
    switch (menuItem.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;
  }
};
<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_edit"
        android:title="@string/action_edit"
        android:orderInCategory="80"
        android:icon="@drawable/ab_edit"
        app:showAsAction="ifRoom" />

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

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

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章