关于在线性布局中以编程方式添加 cardview

About adding cardview programatically inside linear layout

我必须创建一个案例,我需要在 activity_main.xml

中的线性布局中插入 5 个类似卡片的结构

activity_main布局是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/viewMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

</LinearLayout>

And the cardview layout is like this :

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card"
    android:layout_height="100dp"
    android:layout_width="250dp"
    android:background="@color/white"
    app:cardCornerRadius="40dp"
    android:orientation="vertical">
    <TextView
        android:id="@+id/title"
        android:text="Title from Android"
        android:textAlignment="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="monospace"/>
    <TextView
        android:id="@+id/desc"
        android:layout_marginTop="40dp"
        android:textAlignment="center"
        android:text="adding layout dynamically"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fontFamily="serif"/>
</androidx.cardview.widget.CardView>

在 mainactivity onCreate() 方法上我有:

super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout parent=(LinearLayout) findViewById(R.id.viewMain);
        LayoutInflater inflater=getLayoutInflater();

        View view=inflater.inflate(R.layout.text_main,parent,true);

        for(int i=0;i<3;i++){
            parent.addView(view,i);  
        }

    }

显示的错误如下:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.workout/com.example.workout.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3735) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3903) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2328) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:255) at android.app.ActivityThread.main(ActivityThread.java:8212) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:632) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049) Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.view.ViewGroup.addViewInner(ViewGroup.java:5377) at android.view.ViewGroup.addView(ViewGroup.java:5192) at android.view.ViewGroup.addView(ViewGroup.java:5132) at com.example.workout.MainActivity.onCreate(MainActivity.java:36) at android.app.Activity.performCreate(Activity.java:8151) at android.app.Activity.performCreate(Activity.java:8135) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3704) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3903)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2328)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:255)  at android.app.ActivityThread.main(ActivityThread.java:8212)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:632)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)

谁能帮我解决这个问题,这样我就可以在我的线性布局中动态插入 5 个类似卡片的结构`

改变

LayoutInflater inflater=getLayoutInflater(); 
View view=inflater.inflate(R.layout.text_main,parent,true);

View view = LayoutInflater.from(this).inflate(R.layout.text_main, null);

并在for循环中添加LayoutInflater代码

MainActivity.java

中添加以下代码
LinearLayout parent=(LinearLayout) findViewById(R.id.viewMain);
for (int i = 0; i < 3; i++) {
    View view = LayoutInflater.from(this).inflate(R.layout.text_main, null);
    TextView title =  view.findViewById(R.id.title);
    TextView desc =  view.findViewById(R.id.desc);
    parent.addView(view);
}