自定义TittleBar




    

        

        
    

    

        

        

    


    

        

        

    


 

 

 

 

 

package com.example.demo;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @author JiaJian.Xu
 */
public class TitleBarView extends RelativeLayout {

    private LinearLayout mLeftLiner, mMiddleLiner,mRightLiner;
    private TextView mLeftTv, mMiddleTv,mRightTv;
    private ImageView mLeftIv, mMiddleIv,mRightIv;
    private RelativeLayout mBackGround;

    public TitleBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.layout_titlebar_view, this);
        mBackGround = inflate.findViewById(R.id.rel_background);

        mLeftLiner = inflate.findViewById(R.id.lin_left);
        mLeftTv = inflate.findViewById(R.id.tv_left);
        mLeftIv = inflate.findViewById(R.id.iv_left);

        mMiddleLiner = inflate.findViewById(R.id.lin_middle);
        mMiddleTv = inflate.findViewById(R.id.tv_middle);
        mMiddleIv = inflate.findViewById(R.id.iv_middle);

        mRightLiner = inflate.findViewById(R.id.lin_right);
        mRightTv = inflate.findViewById(R.id.tv_right);
        mRightIv = inflate.findViewById(R.id.iv_right);
    }

    /**
     * Background Setting
     */
    public TitleBarView setBackGroundColor(int backGroundColor) {
        mBackGround.setBackgroundColor(getResources().getColor(backGroundColor));
        return this;
    }

    /**
     * Left Setting
     */
    public TitleBarView setLeftDefalutProperty(final Activity activity) {
        mLeftIv.setImageResource(R.mipmap.icon_back);
        mLeftTv.setText("返回");
        mLeftTv.setTextSize(16);
        mLeftTv.setTextColor(getResources().getColor(R.color.white));
        mLeftLiner.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });
        return this;
    }

    public TitleBarView setmLeftProperty(int leftImage, String leftText, int leftTextSize, int leftTextCorlor) {
        mLeftIv.setImageResource(leftImage);
        mLeftTv.setText(leftText);
        mLeftTv.setTextSize(leftTextSize);
        mLeftTv.setTextColor(leftTextCorlor);
        return this;
    }

    public TitleBarView isShowLeftLiner(boolean isShowLeft) {
        if (isShowLeft) {
            mLeftLiner.setVisibility(VISIBLE);
        } else {
            mLeftLiner.setVisibility(GONE);
        }
        return this;
    }

    public TitleBarView setLeftOnClickListener(final OnLeftClickListener listener) {
        mLeftLiner.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClickListener();
            }
        });
        return this;
    }

    public interface OnLeftClickListener {
        void onClickListener();
    }

    /**
     * Middle Setting
     */
    public TitleBarView setMiddleProperty(int middleImage, String middleText,int middleTextSize, int middleTextCorlor) {
        mMiddleIv.setImageResource(middleImage);
        mMiddleTv.setText(middleText);
        mMiddleTv.setTextSize(middleTextSize);
        mMiddleTv.setTextColor(middleTextCorlor);
        return this;
    }

    public TitleBarView isShowMiddleLiner(boolean isShowMiddle) {
        if (isShowMiddle) {
            mMiddleLiner.setVisibility(VISIBLE);
        } else {
            mMiddleLiner.setVisibility(GONE);
        }
        return this;
    }

    public TitleBarView setMiddleOnClickListener(final OnMiddleClickListener listener) {
        mMiddleLiner.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClickListener();
            }
        });
        return this;
    }

    public interface OnMiddleClickListener {
        void onClickListener();
    }

    /**
     * Right Setting
     */
    public  TitleBarView setRightProperty(int rightImage, String rightText,int rightTextSize, int rightTextCorlor){
        mRightIv.setImageResource(rightImage);
        mRightTv.setText(rightText);
        mRightTv.setTextSize(rightTextSize);
        mRightTv.setTextColor(rightTextCorlor);
        return this;
    }

    public TitleBarView isShowRightLiner(boolean isShowRight) {
        if (isShowRight) {
            mRightLiner.setVisibility(VISIBLE);
        } else {
            mRightLiner.setVisibility(GONE);
        }
        return this;
    }

    public TitleBarView setRihtOnClickListener(final OnRightClickListener listener) {
        mRightLiner.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClickListener();
            }
        });
        return this;
    }

    public interface OnRightClickListener {
        void onClickListener();
    }
}

你可能感兴趣的:(自定义TittleBar)