ARTICLE AD BOX
I’ve been trying to create a custom CardView with a title and below the title any type of content, but, when I try overriding addView in the Kotlin class of the custom element to redirect the child, it doesn’t allow it. Does anyone have any solution? Here is the code:
XML:
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:parentTag="androidx.cardview.widget.CardView"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="8dp"> <TextView android:id="@+id/title" style="@style/TextView.Title" android:layout_width="match_parent" android:layout_height="wrap_content" tools:text="Título de ejemplo" /> <FrameLayout android:id="@+id/childrenContainer" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </merge>KOTLIN:
class TarjetaTitle @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : CardView(context, attrs, defStyleAttr) { private var title: TextView private var childrenContainer: FrameLayout init { LayoutInflater.from(context).inflate(R.layout.element_tarjeta_title, this, true) title = findViewById(R.id.title) childrenContainer = findViewById(R.id.childrenContainer) context.obtainStyledAttributes(attrs, R.styleable.TarjetaTitle, defStyleAttr, 0).apply { try { title.text = getString(R.styleable.TarjetaTitle_title) val titleStyle = getResourceId(R.styleable.TarjetaTitle_titleStyle, -1) if (titleStyle != -1) title.setTextAppearance(titleStyle) } finally { recycle() } } } override fun addView(child: View?, params: LayoutParams?) { childrenContainer.addView(child, params) } }