【问题标题】:How to insert a layout into another frameLayout?如何将布局插入另一个框架布局?
【发布时间】:2021-08-10 00:07:09
【问题描述】:

我正在尝试将资源布局插入框架布局,但失败了。框架布局在activity_main.xml中

我的框架布局是(frame_result_contact.xml):

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frame"
    app:layout_constraintTop_toTopOf="parent"/>

我要插入的布局是:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="wrap_content">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="350dp"
    android:orientation="vertical">

    <ImageView
        android:layout_margin="10dp"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:src="@drawable/ic_user"
        android:layout_gravity="center"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textSize="34sp"
        android:textAlignment="center"
        android:id="@+id/contact_name"
        android:layout_gravity="center"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:textSize="24sp"
        android:textAlignment="center"
        android:id="@+id/contact_info"
        android:layout_gravity="center"/>

</LinearLayout>

</androidx.cardview.widget.CardView>

我的代码是:

LayoutInflater inflater = (LayoutInflater) Objects.requireNonNull(getActivity()).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view  = inflater.inflate(R.layout.frame_result_contact,frame,true);
        frame.addView(view);

这是我的错误代码:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

我只是不明白孩子的父母是什么?如何将该布局插入到框架布局中?

【问题讨论】:

    标签: android layout view frame android-framelayout


    【解决方案1】:

    尝试替换这个:View view = inflater.inflate(R.layout.frame_result_contact,frame,true);

    有了这个:View view = inflater.inflate(R.layout.frame_result_contact,frame,false);

    【讨论】:

      猜你喜欢
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 2020-02-29
      • 1970-01-01
      • 2017-07-06
      相关资源
      最近更新 更多