2017-10-08 59 views
3

我想在另一個ConstraintLayout中以編程方式放置一個ConstraintLayout。但是它沒有出現,如果我用之前用RelativeLayout完成的方式膨脹它。在另一個ConstraintLayout中膨脹ConstraintLayout

下面的代碼內文件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); 

但是,當我在內部改變。 xml將innerArea的layout_width或layout_height轉換爲wrap_content或match_parent,沒有任何反應。但是當我改變到2000dp時,內部佈局顯示出來。

我在做什麼錯誤或失蹤?

+0

你用'container'膨脹爲母體的'膨脹()'但是你將膨脹的佈局添加到'parentView'。這看起來不正確。知道'rootView'和'container'是什麼會有幫助。這看起來像片段的'onCreateView()'的一部分,所以看片段事務也可能有幫助。 – Cheticamp

回答

1

感謝Cheticamp指引我在正確的方向。我被充氣ConstraintLayout與根佈局,而不是與約束佈局它應該是因此正確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);