自定义TopBar

2019-07-15  本文已影响0人  努力生活的西鱼

自定义TopBar

public class TopBar extends RelativeLayout {

    private String mTitle;
    private float mTitleTextSize;
    private int mTitleTextColor;

    private String mLeftText;
    private int mLeftTextColor;
    private Drawable mLeftBackground;

    private String mRightText;
    private int mRightTextColor;
    private Drawable mRightBackground;

    private TextView mTitleView;
    private Button mLeftButton;
    private Button mRightButton;

    private LayoutParams mTitleParams, mLeftParams, mRightParams;

    public TopBar(Context context) {
        super(context);
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initProps(context, attrs);
    }

    public TopBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initProps(Context context, AttributeSet attrs) {
        // 通过这个方法,将你在attrs.xml中定义的declare-styleable
        // 的所有属性的值储存到TypedArray中
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TopBar);

        // 从TypedArray中取出对应的值来为要设置的属性赋值
        mTitle = typedArray.getString(R.styleable.TopBar_title);
        mTitleTextSize = typedArray.getDimension(R.styleable.TopBar_titleTextSize, 10);
        mTitleTextColor = typedArray.getColor(R.styleable.TopBar_titleTextColor, 0);
        mLeftText = typedArray.getString(R.styleable.TopBar_leftText);
        mLeftTextColor = typedArray.getColor(R.styleable.TopBar_leftTextColor, 0);
        mLeftBackground = typedArray.getDrawable(R.styleable.TopBar_leftBackground);
        mRightText = typedArray.getString(R.styleable.TopBar_rightText);
        mRightTextColor = typedArray.getColor(R.styleable.TopBar_rightTextColor, 0);
        mRightBackground = typedArray.getDrawable(R.styleable.TopBar_rightBackground);

        // 获取完TypedArray的值后一定要调用recycle()方法来避免重新创建的时候的错误
        typedArray.recycle();

        initView(context);
    }

    private void initView(Context context) {
        mTitleView = new TextView(context);
        mLeftButton = new Button(context);
        mRightButton = new Button(context);

        // 为创建的组件元素赋值
        // 值就来源于我们在引用的xml文件中给对应属性的赋值
        mTitleView.setText(mTitle);
        mTitleView.setTextColor(mTitleTextColor);
        mTitleView.setTextSize(mTitleTextSize);
        mTitleView.setGravity(Gravity.CENTER);

        mLeftButton.setText(mLeftText);
        mLeftButton.setTextColor(mLeftTextColor);
        mLeftButton.setBackground(mLeftBackground);

        mRightButton.setText(mRightText);
        mRightButton.setTextColor(mRightTextColor);
        mRightButton.setBackground(mRightBackground);

        // 为组件元素设置相应的布局元素
        mTitleParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        mTitleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
        // 添加到ViewGroup
        addView(mTitleView, mTitleParams);

        mLeftParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        mLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
        addView(mLeftButton, mLeftParams);

        mRightParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        mRightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
        addView(mRightButton, mRightParams);

        // 左边设置
        mLeftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.leftClick();
            }
        });

        mRightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.rightClick();
            }
        });

    }

    private topbarClickListener mListener;

    public void setTopbarClickListener(topbarClickListener mListener) {
        this.mListener = mListener;
    }

    public interface topbarClickListener {

        void leftClick();

        void rightClick();

    }
}
上一篇 下一篇

猜你喜欢

热点阅读