如何在卡片视图中添加布局?

时间:2018-09-28 00:54:09

标签: java android android-layout

我有一个task_layout.xml,我想以编程方式将其添加到已经使用recyclerView进行修改的卡片视图布局文件中,该文件与xml文件中的<include>命令配合得很好< / p>

与recyclerView相适应的Cardview_layout.xml

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

布局task_layout.xml我想以编程方式添加,我尝试了很多事情,但总是崩溃

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

在我键入的主要活动中,但仍然无法正常工作,即使xml文件中的<include>标记正常工作,它也始终会使应用程序崩溃

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

1 个答案:

答案 0 :(得分:1)

尝试一下...

  

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

为布局创建对象

  

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

     

cardView.addView(child1);