gpt4 book ai didi

java - 如何将布局添加到卡片 View 中?

转载 作者:行者123 更新时间:2023-12-02 10:46:20 25 4
gpt4 key购买 nike

我有一个task_layout.xml我想以编程方式将其添加到已经与 recyclerView 相适应的卡片 View 布局文件中,该文件与 <include> 配合得很好xml 文件中的命令

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

在主要 Activity 中我输入了该内容,但仍然无法正常工作,它总是使应用程序崩溃,甚至是 <include> xml 文件中的标记正在工作

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

最佳答案

试试这个...

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

为您的布局创建一个对象

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

cardView.addView(child1);

关于java - 如何将布局添加到卡片 View 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52546877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com