自定义组件不再正确呈现

Custom component not rendering properly anymore

我是在 android 上创建自定义组件的新手,这是一次有趣、烦人但非常有教育意义的经历。我能够制作相当复杂的自定义组件,我对此非常满意。但是在我将它移到一个目录并移出它之后,它不再显示任何内容。

我的项目由很多 fragments 组成,其中一个使用了我的自定义组件。因此,当我将所有这些 fragments 移动到目录中时,AS 告诉我有人无法在我的自定义组件 class 上找到某些内容。所以我将自定义组件 class 包含在片段目录中。一切正常,但是当我尝试在不是 fragment 的不同布局上使用自定义视图时,自定义组件不显示任何内容。我将自定义视图的每个变量 class 设为 public,然后将其移出片段文件夹。现在,它不再显示任何内容。请指出我做错了什么。

这是自定义组件 class。

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;

    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));

        ta.recycle();
    }

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

        canvas.drawRect(mRect, mPaint);
    }
}

这是我的布局。

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/lytMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/border_bg">

        <ImageView
            android:id="@+id/imgItemThumbnail"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:srcCompat="@tools:sample/avatars[9]"
            tools:visibility="gone" />

        <TextView
            android:id="@+id/lblItemAbbrevName"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:gravity="center"
            android:textSize="16sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/lblItemFullName"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginStart="1dp"
            android:layout_marginEnd="1dp"
            android:gravity="center"
            android:textAlignment="center"
            android:textSize="12sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/lblItemAbbrevName"
            app:layout_constraintVertical_bias="0.0" />

    </android.support.constraint.ConstraintLayout>

</merge>

这是我的attrs.xml。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CheckOutItem">
        <attr name="thumbnail" format="string"/>
        <attr name="abbrevText" format="string"/>
        <attr name="abbrevTextSize" format="dimension"/>
        <attr name="abbrevTextColor" format="color|reference"/>
        <attr name="fullNameText" format="string"/>
        <attr name="fullNameTextSize" format="dimension"/>
        <attr name="fullNameTextColor" format="color|reference"/>
        <attr name="textStyle">
            <enum name="normal" value="0"/>
            <enum name="bold" value="1"/>
            <enum name="italic" value="2"/>
        </attr>
        <attr name="fullNameBackgroundColor" format="color|reference"/>
        <attr name="mainBackgroundColor" format="color|reference"/>
    </declare-styleable>
</resources>

我就是这样使用的。

<com.example.android.projectname.CheckOutItem
        android:id="@+id/coi2"
        android:layout_width="102dp"
        android:layout_height="102dp"
        android:layout_margin="7.8dp"
        app:abbrevText="MP"
        app:abbrevTextColor="@color/black"
        app:abbrevTextSize="12sp"
        app:fullNameBackgroundColor="@color/colorAccent"
        app:fullNameText="Make Payment"
        app:fullNameTextColor="@color/white"
        app:fullNameTextSize="8sp"
        app:mainBackgroundColor="@color/transparent"
        app:thumbnail="" />

您正在为您的布局充气两次

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

删除 init

中的第二个 inflate
private void init(@Nullable AttributeSet set){
    // inflate(ctx, R.layout.checkout_item, this); // remove this line
    ...