![](/img/trans.png)
[英]layout with fragment and framelayout replaced by another fragment and framelayout
[英]How to insert a layout into another frameLayout?
我正在尝试将资源布局插入框架布局但失败了。 框架布局在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.
我只是不明白孩子的父母是什么? 如何将该布局插入框架布局?
尝试替换这个: View view = inflater.inflate(R.layout.frame_result_contact,frame,true);
有了这个: View view = inflater.inflate(R.layout.frame_result_contact,frame,false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.