简体   繁体   中英

How to add layout into card view?

i have a task_layout.xml and i wanted to add Programmatically it to card view layout file that already adapted with recyclerView which works so good with <include> command in xml file

Cardview_layout.xml that adapted with recyclerView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.CardView
    android:id="@+id/CARD_VIEW"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginVertical="8dp"
    android:layout_marginHorizontal="25dp"
    android:elevation="8dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp">

        <TextView
            android:id="@+id/LIST_NAME_TEXT_VIEW"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="Groceries"
            android:fontFamily="@font/segoeuib"
            android:textSize="21sp"
            android:textColor="#000000" />
    <!-- Here where i want to add the child layout -->
    </RelativeLayout>

</android.support.v7.widget.CardView>

the layout task_layout.xml i want to add programmatically and i tried a lot of things and it always crash

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp">

    <CheckBox
        android:id="@+id/CHECK_B0X_TASK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:allowUndo="true"
        android:text="Task1"
        android:textSize="17sp"
        android:fontFamily="@font/segoeui"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"/>
    <TextView
        android:id="@+id/TEXT_CLOCK_TASK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="9:00 AM"
        android:textSize="17sp"
        android:fontFamily="@font/segoeui"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"/>

</RelativeLayout>

in the main activity i typed that but still not working it always crash the application even the <include> tag in the xml file is working

CardView cardView = findViewById(R.id.CARD_VIEW);
    View child1 = LayoutInflater.from(this).inflate(R.layout.task_layout,null);
    cardView.addView(child1);

Try this...

CardView cardView = findViewById(R.id.id_cardview);

Create an object to your layout

View child1 = LayoutInflater.from(this).inflate( R.layout.extra, null);

cardView.addView(child1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM