自定義ActionBar

前言:

相信大家都用過ActionBar吧,基本上都會去繼承AppCompactActivity,但是在某些情況下,想實(shí)現(xiàn)一些特殊的效果的時(shí)候,系統(tǒng)自帶的ActionBar就顯得有點(diǎn)兒雞肋了,不過后面又出了ToolBar控件,大大的彌補(bǔ)了ActionBar的不足,所以本次的自定義ActionBar就是繼承的ToolBar,從而實(shí)現(xiàn)了ActionBar的高度以及可顯示內(nèi)容的高度定制。

下面列舉幾種自定義的樣式:

1、自定義左側(cè)返回菜單,使用方法見代碼:
<pre>
private MyActionBar actionBar;
actionBar = (MyActionBar) findViewById(R.id.myactionbar);
actionBar.withTitle("分類目錄") //設(shè)置title文字
.setABCallBack(this) // 設(shè)置左側(cè)返回按鈕,中間自定義菜單及右側(cè)按鈕點(diǎn)擊事件
.isShowBack(false) //是否顯示左側(cè)返回菜單
//效果見下圖:

</pre>

2、自定義ActionBar中間的view

<pre>
private MyActionBar actionBar;
actionBar = (MyActionBar) findViewById(R.id.myactionbar);
//自定義中間的view。
View view = getLayoutInflater().inflate(R.layout.actionbar_center, null);
view.findViewById(R.id.btn_click).setOnClickListener(v -> Toast.makeText(getApplicationContext(), "id:" + ((Button) v.findViewById(R.id.btn_click)).getText(), Toast.LENGTH_SHORT).show());
actionBar.setABCallBack(this) // 設(shè)置左側(cè)返回按鈕,中間自定義菜單及右側(cè)按鈕點(diǎn)擊事件
.isShowBack(false) //是否顯示左側(cè)返回菜單
.addCenterView(view); //添加自定義view 見圖二TEST按鈕
//效果見下圖:

</pre>

3、自定義右側(cè)按鈕點(diǎn)擊事件

<pre>
private MyActionBar actionBar;
actionBar = (MyActionBar) findViewById(R.id.myactionbar);
//右側(cè)item adapter
List<MyPopupWindow.MenuEntity> menuEntities = new ArrayList<>();
menuEntities.add(new MyPopupWindow.MenuEntity(R.drawable.titlebar_back_press, "name"));
menuEntities.add(new MyPopupWindow.MenuEntity(R.drawable.titlebar_back_press, "name1"));
menuEntities.add(new MyPopupWindow.MenuEntity(R.drawable.titlebar_back_press, "name2"));

actionBar.setABCallBack(this) // 設(shè)置左側(cè)返回按鈕,中間自定義菜單及右側(cè)按鈕點(diǎn)擊事件
.isShowBack(false) //是否顯示左側(cè)返回菜單
.addMenuList(menuEntities) // 添加右側(cè)按鈕item
.addCenterView(view); //添加自定義view
//效果見下圖:
</pre>

MyActionBar 源碼:

<pre>
public class MyActionBar extends Toolbar {

private int backColor;
private int defaultColor;
private TextView tvTitle;
private ImageView ivBack, ivRight;
private RelativeLayout llBackground;
private AbCallBack abCallBack;
private Context mContext;
private LinearLayout llcontainer;
private MyPopupWindow popupWindow;

public MyActionBar(Context context) {
    super(context, null);

}

public MyActionBar(Context context, final AttributeSet attrs) {
    this(context, attrs, -1);
}

@TargetApi(Build.VERSION_CODES.M)
public MyActionBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.mContext = context;
    defaultColor = context.getColor(R.color.colorPrimary);
    LayoutInflater.from(context).inflate(R.layout.actionbar, this, true);
    TypedArray typedArray = context.obtainStyledAttributes(attrs,
            R.styleable.MyActionBar, 0, 0);
    backColor = typedArray.getColor(R.styleable.MyActionBar_myactionbar_background, defaultColor);
    llBackground = (RelativeLayout) findViewById(R.id.ll_background);
    llBackground.setBackgroundColor(backColor);
    llcontainer = (LinearLayout) findViewById(R.id.ll_container);
    tvTitle = (TextView) findViewById(R.id.tv_title);
    ivBack = (ImageView) findViewById(R.id.iv_back);
    ivRight = (ImageView) findViewById(R.id.iv_right);
    ivBack.setColorFilter(getResources().getColor(R.color.white));
    ivBack.setOnClickListener(v -> abCallBack.onBackClick());
    ivRight.setColorFilter(getResources().getColor(R.color.white));
    ivRight.setOnClickListener(v -> popupWindow.show(this));
}

public MyActionBar addCenterView(View view) {
    if (llcontainer != null) {
        llcontainer.removeAllViews();
        llcontainer.addView(view);
    }
    return this;
}

public MyActionBar addCenterViewClickListener(View view) {
    abCallBack.onCenterViewClick(view);
    return this;
}

public MyActionBar setHomeIcon(int resId) {
    if (mContext != null) {
        Picasso.with(mContext)
                .load(resId)
                .error(R.drawable.titlebar_back_press)
                .into(ivBack);
    }
    return this;
}


public MyActionBar isShowBack(boolean isShow) {
    if (!isShow) {
        ivBack.setVisibility(INVISIBLE);
    } else {
        ivBack.setVisibility(VISIBLE);
    }
    return this;
}

public MyActionBar addMenuList(List<MyPopupWindow.MenuEntity> menuEntityList) {
    popupWindow = new MyPopupWindow(mContext);
    popupWindow.addMenuList(menuEntityList)
            .addOnItemClickLisenter((parent, view, position, id) -> {
                abCallBack.onRightClick(position);
                popupWindow.dismiss();
            }).build();

    return this;
}

public MyActionBar setABCallBack(AbCallBack abCallBack) {
    this.abCallBack = abCallBack;
    return this;
}

public MyActionBar withTitle(String title) {
    tvTitle.setText(title);
    return this;
}

}
</pre>

AbCallBack (ActionBar點(diǎn)擊事件回調(diào))源碼:
<pre>
public interface AbCallBack {
void onBackClick();
void onRightClick();
}
</pre>

更多使用方法詳見github:

github : https://github.com/momentslz/openobj

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容