Android--ToolBar使用

官方文檔對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);
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容