Android開發自定義組合控件

public class MyRegItemView extends RelativeLayout{

    private TextView leftTextView;
    private TextView rightTextView;
    private ImageView rightImageView;

    public MyRegItemView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.item_reg_baseinfo, this, true);
        leftTextView = (TextView)findViewById(R.id.tv_left_msg);
        rightTextView = (TextView)findViewById(R.id.tv_right_msg);
        rightImageView = (ImageView)findViewById(R.id.iv_right);

        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.MyRegItemView);
        if(attributes != null){
            String leftMsg = attributes.getString(R.styleable.MyRegItemView_leftMsg);
            String rightMsg = attributes.getString(R.styleable.MyRegItemView_rightMsg);
            int rightImageSrc = attributes.getResourceId(R.styleable.MyRegItemView_rightImageSrc,R.mipmap.ic_right);
            rightImageView.setImageResource(rightImageSrc);
            if(!TextUtils.isEmpty(leftMsg)){
                leftTextView.setText(leftMsg);
            }
            if(!TextUtils.isEmpty(rightMsg)){
                rightTextView.setText(rightMsg);
            }


            attributes.recycle();
        }

    }

    public void setRightResult(String result){
        if(!TextUtils.isEmpty(result)){
            rightTextView.setText(result);
        }
    }



}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章