ActionBar 透明


FadingActionBarHelper.java

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;

public class FadingActionBarHelper {
	  private static final String TAG = "FadingActionBarHelper";

	    private int mAlpha = 255;
	    private Drawable mDrawable;
	    private boolean isAlphaLocked;

	    private final ActionBar mActionBar;

	    public FadingActionBarHelper(final ActionBar actionBar) {
	        mActionBar = actionBar;
	    }

	    public FadingActionBarHelper(final ActionBar actionBar, final Drawable drawable) {
	        mActionBar = actionBar;
	        setActionBarBackgroundDrawable(drawable);
	    }

	    public void setActionBarBackgroundDrawable(Drawable drawable) {
	        setActionBarBackgroundDrawable(drawable, true);
	    }

	    @TargetApi(Build.VERSION_CODES.KITKAT)
		public void setActionBarBackgroundDrawable(Drawable drawable, boolean mutate) {
	        mDrawable = mutate ? drawable.mutate() : drawable;
	        mActionBar.setBackgroundDrawable(mDrawable);
	        if (mAlpha == 255) {
	            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
	                mAlpha = mDrawable.getAlpha();
	        } else {
	            setActionBarAlpha(mAlpha);
	        }
	    }

	    /**
	     * An {@link android.app.ActionBar} background drawable.
	     *
	     * @see #setActionBarBackgroundDrawable(android.graphics.drawable.Drawable)
	     * @see #setActionBarAlpha(int)
	     */
	    public Drawable getActionBarBackgroundDrawable() {
	        return mDrawable;
	    }

	    /**
	     * Please use this method for global changes only!
	     * This is helpful when you need to provide something like
	     * Navigation drawer: lock ActionBar and set
	     * {@link android.graphics.drawable.Drawable#setAlpha(int)}
	     * to {@link #getActionBarBackgroundDrawable()} directly.
	     *
	     * @param alpha a value from 0 to 255
	     * @see #getActionBarBackgroundDrawable()
	     * @see #getActionBarAlpha()
	     */
	    public void setActionBarAlpha(int alpha) {
	        if (mDrawable == null) {
	            return;
	        }
	        if (!isAlphaLocked) {
				mDrawable.setAlpha(alpha);
				View view = mActionBar.getCustomView();
				if(view!=null){
					//这里是对自定义actionbar背景的处理,我这边就草草了事了
					if(alpha>=55){
					//	view.findViewById(R.id.search_button).setBackgroundResource(R.drawable.search);
					//	view.findViewById(R.id.refresh_button).setBackgroundResource(R.drawable.refresh);
					}else{
						//view.findViewById(R.id.search_button).setBackgroundResource(R.drawable.skin_nav_icon_l_search_rev);
					//	view.findViewById(R.id.refresh_button).setBackgroundResource(R.drawable.skin_nav_icon_r_refresh_rev);
					}
					
				}
			}
	        mAlpha = alpha;
	    }

	    public int getActionBarAlpha() {
	        return mAlpha;
	    }

	    /**
	     * When ActionBar's alpha is locked {@link #setActionBarAlpha(int)}
	     * won't change drawable's alpha (but will change {@link #getActionBarAlpha()} level)
	     *
	     * @param lock
	     */
	    public void setActionBarAlphaLocked(boolean lock) {

	        // Update alpha level on unlock
	        if (isAlphaLocked != (isAlphaLocked = lock) && !isAlphaLocked) {
	            setActionBarAlpha(mAlpha);
	        }
	    }

	    public boolean isActionBarAlphaLocked() {
	        return isAlphaLocked;
	    }
}
使用方法 写在onCreate里

        // ActionBar 透明
        this.getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

        setContentView(R.layout.xxxxxx);

        ActionBar actionBar = getActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayUseLogoEnabled(false);
        // 设置actionbar透明
        FadingActionBarHelper helper = new FadingActionBarHelper(actionBar, new ColorDrawable(
                getResources().getColor(android.R.color.holo_orange_light)));

        helper.setActionBarAlpha(255);


重点在于这三行
        this.getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);


        FadingActionBarHelper helper = new FadingActionBarHelper(actionBar, new ColorDrawable(
                getResources().getColor(android.R.color.holo_orange_light)));


        helper.setActionBarAlpha(255);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值