Android移動開發-在Android 5.0 以上版本自定義Toolbar的實現

Android移動開發-在Android 5.0 以上版本自定義Toolbar的實現,Google在Android 5.0 以上版本新增瞭自定義Toolbar的功能,對於開發者是一件喜事。相比於Actionbar,Toolbar最明顯的一點就是變得很自由,可隨處放置。

使用Toolbar之前先在build.gradle引入v7支持包

dependencies {
    ...
    compile 'com.android.support:appcompat-v7:26.1.0'
    compile 'com.android.support:palette-v7:26.1.0'
}

在styles.xml文件裡把Actionbar去掉

@color/colorPrimary
        @color/colorPrimaryDark
        @color/colorAccent

layout/mytoolbar.xml界面佈局代碼如下:



  

layout/activity_toolbar.xml界面佈局代碼如下:




    

    
        
        

            


        
        
        

            

        

    

menu/main.xml界面佈局代碼如下:



	   

ToolbarActivity.java邏輯代碼如下:

package com.fukaimei.moontoolbar;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;


public class ToolbarActivity extends AppCompatActivity {
    private Toolbar mToolbar;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private TextView tv_close;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toolbar);
        initViews();
    }

    private void initViews() {
        tv_close = (TextView) this.findViewById(R.id.tv_close);
        mToolbar = (Toolbar) this.findViewById(R.id.toolbar);
        mToolbar.setTitle("Toolbar");

        setSupportActionBar(mToolbar);
        //是否給左上角圖標的左邊加上一個返回的圖標
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setLogo(R.drawable.ic_launcher);
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_settings:
                        Toast.makeText(ToolbarActivity.this, "設置動作", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.action_share:
                        Toast.makeText(ToolbarActivity.this, "分享動作", Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
        //設置側或佈局
        mDrawerLayout = (DrawerLayout) this.findViewById(R.id.id_drawerlayout);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open,
                R.string.drawer_close);
        mDrawerToggle.syncState();
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        tv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawerLayout.closeDrawer(Gravity.LEFT);
            }
        });
        //使用Patette
        setPatette();
    }

    private void setPatette() {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
        Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(Palette palette) {
                Palette.Swatch swatch = palette.getVibrantSwatch();
                getSupportActionBar().setBackgroundDrawable(new ColorDrawable(swatch.getRgb()));

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

Demo程序運行效果界面截圖如下:

這裡寫圖片描述 這裡寫圖片描述

You May Also Like