public class InputBtnView extends RelativeLayout {
private final TextView mTvLeft;
private final TextView mTvRight;
private final ImageView mIvRight;
public InputBtnView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.btn_input, this, true).setBackgroundColor(Color.WHITE);
mTvLeft = findViewById(R.id.btn_input_tv_left);
mTvRight = findViewById(R.id.btn_input_tv_right);
mIvRight = findViewById(R.id.btn_input_iv_right);
View mLine = findViewById(R.id.btn_input_line);
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.BtnInput);
if (attributes !=null) {
String leftStr =attributes.getString(R.styleable.BtnInput_btn_tv_left_text);
if (!TextUtils.isEmpty(leftStr)) {
mTvLeft.setText(leftStr);
}
String rightStr =attributes.getString(R.styleable.BtnInput_btn_tv_right_text);
if (!TextUtils.isEmpty(rightStr)) {
mTvRight.setText(rightStr);
}
if (attributes.getBoolean(R.styleable.BtnInput_btn_iv_right_show, true)){
mIvRight.setImageResource(attributes.getResourceId(R.styleable.BtnInput_btn_iv_right_src,R.mipmap.into));
mIvRight.setVisibility(View.VISIBLE);
}else{
mIvRight.setVisibility(View.GONE);
}
mTvLeft.setTextColor(attributes.getColor(R.styleable.BtnInput_btn_tv_left_color, ContextCompat.getColor(context,R.color.colorBtnTvLeft)));
mTvRight.setTextColor(attributes.getColor(R.styleable.BtnInput_btn_tv_right_color, ContextCompat.getColor(context,R.color.colorBtnTvRight)));
if (attributes.getBoolean(R.styleable.BtnInput_btn_line_show,true)){
mLine.setVisibility(View.VISIBLE);
}else{
mLine.setVisibility(View.GONE);
}
attributes.recycle();
}
}
public void setTvLeftText(String content){
mTvLeft.setText(content);
}
public void setTvRightText(String content){
mTvRight.setText(content);
}
public void setTvLeftColor(int color){
mTvLeft.setTextColor(color);
}
public void setTvRightColor(int color){
mTvRight.setTextColor(color);
}
public void setIvRightShow(boolean show){
if (show){
mIvRight.setVisibility(View.VISIBLE);
}else{
mIvRight.setVisibility(View.GONE);
}
}
public void setIvRightBitmap(Bitmap bitmap){
mIvRight.setImageBitmap(bitmap);
}
}
attrs:
name="btn_tv_left_color" format="color"
name="btn_tv_right_color" format="color"
"btn_tv_left_text" format="string"
"btn_tv_right_text" format="string"
name="btn_iv_right_src" format="reference|integer"
name="btn_iv_right_show" format="boolean"
name="btn_line_show" format="boolean"