android – 在另一个ConstraintLayout中膨胀ConstraintLayout

我想以编程方式将ConstraintLayout放在另一个ConstraintLayout中.但它没有出现,如果我按照我之前使用RelativeLayout的方式进行充气.

这是代码内部文件inner_area.xml:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/innerArea"
android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageView
    android:id="@+id/grade_one"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:layout_marginTop="0dp"
    android:background="@drawable/onegreen"
    app:layout_constraintBottom_toTopOf="@+id/h1"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/v1"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    tools:layout_conversion_absoluteHeight="20dp"
    tools:layout_conversion_absoluteWidth="30dp"
    tools:layout_conversion_absoluteX="0dp"
    tools:layout_conversion_absoluteY="0dp" />

<ImageView
    android:id="@+id/grade_three"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="0dp"
    android:layout_marginRight="0dp"
    android:layout_marginTop="0dp"
    android:background="@drawable/threelila"
    app:layout_constraintBottom_toTopOf="@+id/h2"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/v1"
    app:layout_constraintTop_toTopOf="@+id/h1"
    app:layout_constraintVertical_bias="0.0"
    tools:layout_conversion_absoluteHeight="20dp"
    tools:layout_conversion_absoluteWidth="30dp"
    tools:layout_conversion_absoluteX="0dp"
    tools:layout_conversion_absoluteY="20dp" />
    .....
 </android.support.constraint.ConstraintLayout>

外部LayoutFile parent.xml:

   <?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.ConstraintLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/parentView"
android:background="@color/primary">

</android.support.constraint.ConstraintLayout>

Javacode:

    ViewGroup parentView = rootView.findViewById(R.id.parentView);
    final ConstraintLayout innerArea = (ConstraintLayout) inflater.inflate(R.layout.inner_area, container, false);
    parentView.addView(innerArea);

但是当我在inner.xml中将innerArea的layout_width或layout_height更改为wrap_content或match_parent时,没有任何反应.但当我改为2000dp时,内部布局显示出来.

我做错了什么或错过了什么?

最佳答案 感谢Cheticamp让我指向正确的方向.我用根布局夸大了ConstraintLayout,而不是它应该在的Constraint Layout.所以正确的Javacode是:

    parentView = rootView.findViewById(R.id.parentView);
    ViewGroup parentView = rootView.findViewById(R.id.parentView);
    final ConstraintLayout innerArea = (ConstraintLayout) 
    inflater.inflate(R.layout.inner_area, parentView, false);
    parentView.addView(innerArea);
点赞