自定义view:经典的标题栏(组合控件)

最近要用到一个公用的标题栏,左中右包含有iamgeview和textview方便扩展,可以根据情况设置点击事件和隐藏或者显示,因为比较常用就放出来,先来个效果图,有需要的拿去基本就能用了



自定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- 标题栏自定义属性 -->
    <declare-styleable name="CommonTitleView">
        <attr name="leftImageView" format="reference"></attr>
        <attr name="leftTextView" format="string"></attr>
        <attr name="middleImageView" format="reference"></attr>
        <attr name="middleTextView" format="string"></attr>
        <attr name="rightImageView" format="reference"></attr>
        <attr name="rightTextView" format="string"></attr>
    </declare-styleable>
</resources>


CommonTitleView布局使用

 <com.example.administrator.myapplication.CommonTitleView
        android:id="@+id/titleView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:middleTextView="个人中心"
        app:leftTextView="返回"
        app:rightImageView="@mipmap/ic_chat"
        app:rightTextView="收件箱">
    </com.example.administrator.myapplication.CommonTitleView>


代码中相关设置示例

   //CommonTitleView点击事件,显示隐藏,图片设置
        titleView = (CommonTitleView) findViewById(R.id.titleView);
        titleView.setMiddleOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(EventBusActivity.this, "个人中心", Toast.LENGTH_SHORT).show();
            }
        });
        titleView.setMiddleTextGone(View.VISIBLE);
        titleView.setRightImage(R.mipmap.ic_chat);


CommonTitleView完整代码

/**公用标题栏
 * Created by cyr on 2016/5/20.
 */
public class CommonTitleView extends LinearLayout {

    private Context mContext;
    //左 中 右
    private ImageView mLeftImageView;
    private TextView mLeftTextView;
    private LinearLayout mLeftLinearLayout;

    private ImageView mMiddleImageView;
    private TextView mMiddleTextView;
    private LinearLayout mMiddleLinearLayout;

    private ImageView mRightImageView;
    private TextView mRightTextView;
    private LinearLayout mRightLinearLayout;

    public CommonTitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView(attrs);
    }

    private void initView(AttributeSet attrs) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.common_title_view, null);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        this.addView(view, lp);

        mLeftImageView = (ImageView) view.findViewById(R.id.iv_title_left);
        mLeftTextView = (TextView) view.findViewById(R.id.tv_title_left);
        mLeftLinearLayout = (LinearLayout) view.findViewById(R.id.ll_title_left);

        mMiddleImageView = (ImageView) view.findViewById(R.id.iv_title_middle);
        mMiddleTextView = (TextView) view.findViewById(R.id.tv_title_middle);
        mMiddleLinearLayout = (LinearLayout) view.findViewById(R.id.ll_title_middle);
        
        mRightImageView = (ImageView) view.findViewById(R.id.iv_title_right);
        mRightTextView = (TextView) view.findViewById(R.id.tv_title_right);
        mRightLinearLayout = (LinearLayout) view.findViewById(R.id.ll_title_right);

        TypedArray array = mContext.obtainStyledAttributes(attrs, R.styleable.CommonTitleView);


        for (int i = 0; i < array.length(); i++) {

            int index = array.getIndex(i);

            switch (index) {
                case R.styleable.CommonTitleView_leftImageView://左图
                    int leftId = array.getResourceId(index, 0);
                    mLeftImageView.setImageResource(leftId);
                    break;
                case R.styleable.CommonTitleView_leftTextView://左字
                    String leftText = array.getString(index);
                    mLeftTextView.setText(leftText);
                    mLeftTextView.setVisibility(VISIBLE);
                    break;
                case R.styleable.CommonTitleView_middleImageView://中图
                    int middleId = array.getResourceId(index, 0);
                    mMiddleImageView.setImageResource(middleId);
                    mMiddleImageView.setVisibility(VISIBLE);
                    break;
                case R.styleable.CommonTitleView_middleTextView://中字
                    String middleText = array.getString(index);
                    mMiddleTextView.setText(middleText);
                    mMiddleTextView.setVisibility(VISIBLE);
                    break;
                case R.styleable.CommonTitleView_rightImageView://右图
                    int rightId = array.getResourceId(index, 0);
                    mRightImageView.setImageResource(rightId);
                    mRightImageView.setVisibility(VISIBLE);
                    break;
                case R.styleable.CommonTitleView_rightTextView://右字
                    String rightText = array.getString(index);
                    mRightTextView.setText(rightText);
                    mRightTextView.setVisibility(VISIBLE);
                    break;

            }
        }
        array.recycle();

        //左边LinearLayout默认返回
        mLeftLinearLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mContext instanceof Activity) {
                    ((Activity)mContext).finish();
                }
            }
        });
    }

    /**
     * 左边监听事件
     * @param listener
     */
    public void setLeftOnClickListener(OnClickListener listener) {
        if (mLeftLinearLayout != null) {
            mLeftLinearLayout.setOnClickListener(listener);
        }
    }

    /**
     * 中间监听事件
     * @param listener
     */
    public void setMiddleOnClickListener(OnClickListener listener) {
        if (mMiddleLinearLayout != null) {
            mMiddleLinearLayout.setOnClickListener(listener);
        }
    }

    /**
     * 右边监听事件
     * @param listener
     */
    public void setRightOnClickListener(OnClickListener listener) {
        if (mRightLinearLayout != null) {
            mRightLinearLayout.setOnClickListener(listener);
        }
    }

    /**
     * 设置左边图片
     * @param id
     */
    public void setLeftImage(int id) {
        if (mLeftImageView != null) {
            mLeftImageView.setImageResource(id);
        }
    }

    /**
     * 设置中间图片
     * @param id
     */
    public void setMiddleImage(int id) {
        if (mMiddleImageView != null) {
            mMiddleImageView.setImageResource(id);
            mMiddleImageView.setVisibility(VISIBLE);
        }
    }

    /**
     * 设置右边图片
     * @param id
     */
    public void setRightImage(int id) {
        if (mRightImageView != null) {
            mRightImageView.setImageResource(id);
            mRightImageView.setVisibility(VISIBLE);
        }
    }

    /**
     * 设置左边文字
     * @param text
     */
    public void setLeftText(String text) {
        if (mLeftTextView != null) {
            mLeftTextView.setText(text);
            mLeftTextView.setVisibility(VISIBLE);
        }
    }

    /**
     * 设置中间文字
     * @param text
     */
    public void setMiddleText(String text) {
        if (mMiddleTextView != null) {
            mMiddleTextView.setText(text);
            mMiddleTextView.setVisibility(VISIBLE);
        }
    }

    /**
     * 设置右边文字
     * @param text
     */
    public void setRightText(String text) {
        if (mRightTextView != null) {
            mRightTextView.setText(text);
            mRightTextView.setVisibility(VISIBLE);
        }
    }


    /**
     * 隐藏左边文字
     * @param
     */
    public void setLeftTextGone(int stauts) {
        if (mLeftTextView != null) {
            mLeftTextView.setVisibility(stauts);
        }
    }

    /**
     * 隐藏中间文字
     * @param
     */
    public void setMiddleTextGone(int stauts) {
        if (mMiddleTextView != null) {
            mMiddleTextView.setVisibility(stauts);
        }
    }

    /**
     * 隐藏右边文字
     * @param
     */
    public void setRightTextGone(int stauts) {
        if (mRightTextView != null) {
            mRightTextView.setVisibility(stauts);
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值