在另一个 ConstraintLayout 中膨胀 ConstraintLayout
Inflating ConstraintLayout in another 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>
外部布局文件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>
Java代码:
ViewGroup parentView = rootView.findViewById(R.id.parentView);
final ConstraintLayout innerArea = (ConstraintLayout) inflater.inflate(R.layout.inner_area, container, false);
parentView.addView(innerArea);
但是当我将 innerArea 的 inner.xml、layout_width 或 layout_height 更改为 wrap_content 或 match_parent 时,没有任何反应。但是当我更改为 2000dp 时,内部布局出现了。
我做错了什么或遗漏了什么?
感谢 Cheticamp 为我指明了正确的方向。我用根布局而不是它应该在的约束布局来膨胀约束布局。所以正确的 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);
我似乎来晚了,但我尝试了很多方法来解决这个问题,但没有任何效果。
我最终只是将内部约束布局放在另一个布局(在我的情况下是相对布局)中,这为我解决了问题。
我想以编程方式将一个 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>
外部布局文件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>
Java代码:
ViewGroup parentView = rootView.findViewById(R.id.parentView);
final ConstraintLayout innerArea = (ConstraintLayout) inflater.inflate(R.layout.inner_area, container, false);
parentView.addView(innerArea);
但是当我将 innerArea 的 inner.xml、layout_width 或 layout_height 更改为 wrap_content 或 match_parent 时,没有任何反应。但是当我更改为 2000dp 时,内部布局出现了。
我做错了什么或遗漏了什么?
感谢 Cheticamp 为我指明了正确的方向。我用根布局而不是它应该在的约束布局来膨胀约束布局。所以正确的 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);
我似乎来晚了,但我尝试了很多方法来解决这个问题,但没有任何效果。
我最终只是将内部约束布局放在另一个布局(在我的情况下是相对布局)中,这为我解决了问题。