Как программно инициализировать пользовательский компонент в Android? - PullRequest
0 голосов
/ 18 мая 2019

Я уже знаю, как использовать пользовательский компонент в XML. Сейчас я пытаюсь вызвать один программно. Я не нашел никакого учебного пособия по этому поводу, поэтому я попытался сделать так, как мы объявляем представление программно. Одна проблема, с которой я столкнулся, заключается в том, как мне получить AttributeSet, который я передаю своему конструктору? Я не знаю, как получить или создать это, чтобы передать его для моего пользовательского компонента.

Это мой класс компонентов.

public class CheckOutItem extends ConstraintLayout {
    public Context ctx;
    public Paint mPaint;
    public Rect mRect;
    int mSquareColor;
    public ImageView imgThumbnail;
    public TextView lblAbbrev, lblFullName;
    public ConstraintLayout lytMain;
    String TAG = "sharePOS";

    public CheckOutItem(Context context) {
        super(context);
        ctx = context;
        init(null);
    }

    public CheckOutItem(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.checkout_item, this);
        ctx = context;

        init(attrs);
    }

    public CheckOutItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        ctx = context;
        init(attrs);
    }

    private void init(@Nullable AttributeSet set){
        //inflate(ctx, R.layout.checkout_item, this);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mRect = new Rect();

        if(set == null){
            return;
        }

        TypedArray ta = getContext().obtainStyledAttributes(set, R.styleable.CheckOutItem);

        this.imgThumbnail = findViewById(R.id.imgItemThumbnail);
        this.lblAbbrev = findViewById(R.id.lblItemAbbrevName);
        this.lblFullName = findViewById(R.id.lblItemFullName);
        this.lytMain = findViewById(R.id.lytMain);

        this.lblAbbrev.setText(ta.getText(R.styleable.CheckOutItem_abbrevText));
        this.lblAbbrev.setTextSize(ta.getDimension(R.styleable.CheckOutItem_abbrevTextSize, 1f));
        this.lblAbbrev.setTextColor(ta.getColor(R.styleable.CheckOutItem_abbrevTextColor, Color.BLACK));

        this.lblFullName.setText(ta.getText(R.styleable.CheckOutItem_fullNameText));
        this.lblFullName.setTextSize(ta.getDimension(R.styleable.CheckOutItem_fullNameTextSize, 1f));
        this.lblFullName.setTextColor(ta.getColor(R.styleable.CheckOutItem_fullNameTextColor, Color.BLACK));
        this.lblFullName.setBackgroundColor(ta.getColor(R.styleable.CheckOutItem_fullNameBackgroundColor, Color.WHITE));

        this.lytMain.setBackgroundColor(ta.getColor(R.styleable.CheckOutItem_mainBackgroundColor, Color.LTGRAY));
        this.lytMain.setBackground(ta.getDrawable(R.styleable.CheckOutItem_mainBackgroundDrawable));

        ta.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mRect.left = 0;
        mRect.right = getWidth();
        mRect.top = 0;
        mRect.bottom = getHeight();

        Log.d(TAG, "rect: " + mRect);
        canvas.drawRect(mRect, mPaint);
    }
}

UPDATE:

Как установить поле в пользовательском компоненте? Обычный LayoutParams с setMargins(), похоже, не работает.

...