自定义组件无法正确呈现

编程入门 行业动态 更新时间:2024-10-09 03:28:06
本文介绍了自定义组件无法正确呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我刚开始在android上创建自定义组件,这是一种有趣,烦人且非常有教育意义的体验.我能够制作非常复杂的自定义组件,对此我感到非常满意.但是,当我将其移至目录并移出目录后,它不再显示任何内容.

I'm new to creating custom components on android and it was a fun, annoying, and very educational experience. I was able to make my quite complex custom component and I'm very happy with it. But after I moved it to a directory and out of it, it doesn't display anything anymore.

我的项目由很多fragments组成,其中一个使用了我的自定义组件.因此,当我将所有这些fragments移到目录中时,AS告诉我在我的自定义组件类上找不到某些内容.因此,我在fragments目录中包括了自定义组件类.一切正常,但是当我尝试在不是fragment的其他布局上使用自定义视图时,自定义组件不会显示任何内容.我将自定义视图类的每个变量都公开,然后将其移到fragments文件夹之外.现在,它不再显示任何内容.请指出我做错了什么.

My project consist of a lot of fragments and my custom component is used on one of them. So when I moved all those fragments in a directory, AS told me that someone can't find something on my custom component class. So I included the custom component class inside the fragments directory. Everything worked fine but when I tried to use the custom view on a different layout which is not a fragment's, the custom component doesn't display anything. I made every variable of the custom view class to public then moved it outside the fragments folder. Now, it doesn't display anything anymore. Please point out what I've done wrong.

这是自定义组件类.

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="schemas.android/apk/res/android" xmlns:app="schemas.android/apk/res-auto" xmlns:tools="schemas.android/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.

This is my 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

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

更多推荐

自定义组件无法正确呈现

本文发布于:2023-11-28 04:36:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640992.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   组件   正确

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!