约束:约束布局:1.1.0不显示动态添加的自定义视图

时间:2018-05-15 13:10:33

标签: android-studio-3.0 android-constraintlayout

Android Studio 3.1,Gradle 4.7

我使用了实施' com.android.support.constraint:constraint-layout:1.0.2'

这是我的xml:

  <android.support.constraint.ConstraintLayout
                        android:id="@+id/categoriesContainer"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="@+id/nameContainer"
                        app:layout_constraintTop_toBottomOf="@+id/birthDateContainer">

                        <android.support.v7.widget.GridLayout
                            android:id="@+id/categoriesGridContainer"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginEnd="15dp"
                            android:layout_marginLeft="15dp"
                            android:layout_marginRight="15dp"
                            android:layout_marginStart="15dp"
                            android:layout_marginTop="@dimen/tile_preview_margin_between_tiles"
                            app:layout_constraintEnd_toEndOf="@+id/categoriesContainer"
                            app:layout_constraintStart_toStartOf="@+id/categoriesContainer"
                            app:layout_constraintTop_toBottomOf="@+id/categoriesContainer" />

                    </android.support.constraint.ConstraintLayout>

此处&#34; profile_category_inactive.xml&#34;

<?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:id="@+id/profileCategoryContainer"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:background="@drawable/profile_category_inactive_bg">

    <TextView
        android:id="@+id/categoryNameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:ellipsize="end"
        android:singleLine="true"
        android:maxLines="1"
        android:text="Test category"
        android:textColor="#a3a3a3"
        android:textSize="15sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

这里使用此容器的java代码段: 我通过容器中的java代码添加动态我的所有自定义视图&#34; categoriesGridContainer&#34;:

GridLayout categoriesGridContainer = findViewById(R.id.categoriesGridContainer);
        categoriesGridContainer.setColumnCount(columnCount);
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        for (int index = 0; index < findCatagoriesList.size(); index++) {
            View currentProfileCategoryActive = null;
            if (index == 1 || index == 2 || index == 5) { // test
                currentProfileCategoryActive = inflater.inflate(R.layout.profile_category_inactive, null, false);
            } else {
                currentProfileCategoryActive = inflater.inflate(R.layout.profile_category_active, null, false);
            }
            categoriesGridContainer.addView(currentProfileCategoryActive);
            ConstraintLayout profileCategoryContainer = currentProfileCategoryActive.findViewById(R.id.profileCategoryContainer);
            GridLayout.LayoutParams params = new GridLayout.LayoutParams(GridLayout.spec(
                    GridLayout.UNDEFINED, GridLayout.FILL, 1f),
                    GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 1f));

            params.width = (int) AndroidUtil.dpToPx(this, categoryItemWidth);
            params.bottomMargin = (int) marginBetweenTilesDp;
            params.topMargin = (int) marginBetweenTilesDp;
            params.rightMargin = (int) marginBetweenTilesDp;
            params.leftMargin = (int) marginBetweenTilesDp;
            profileCategoryContainer.setLayoutParams(params);
            TextView categoryNameTextView = currentProfileCategoryActive.findViewById(R.id.categoryNameTextView);
            Category category = findCatagoriesList.get(index);
            String categoryName = LocalizedStringUtil.getLocalizedStringValue(category.getName());
            categoryNameTextView.setText(categoryName);
        }

它的工作正常。在容器&#34; categoriesGridContainer&#34;显示我的所有观点。

但是如果我将lib更新为

implementation 'com.android.support.constraint:constraint-layout:1.1.0'

结果是容器&#34; categoriesGridContainer&#34;现在是空的。 不显示我通过java代码添加动态的所有自定义视图。

1 个答案:

答案 0 :(得分:0)

由于您在转换为ConstraintLayout的更高版本后出现了问题,因此您可能遇到过以前允许的问题,而不是现在或者只是处理不同的问题。我会先清理初始的XML布局。我做了一些改变。看看它是否有帮助。

<android.support.constraint.ConstraintLayout
    android:id="@+id/categoriesContainer"
    <!-- Don't use match_parent but 0dp and rely on the constraints to expand -->
    <!-- I assume the parent is also a ConstraintLayout. -->
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/nameContainer"
    app:layout_constraintTop_toBottomOf="@+id/birthDateContainer">

    <android.support.v7.widget.GridLayout
        android:id="@+id/categoriesGridContainer"
        <!-- No match_parent -->
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginStart="15dp"
        android:layout_marginTop="@dimen/tile_preview_margin_between_tiles"
        <!-- Reference should be to parent and not the parent's id.
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        <!-- I question the following constraint since it will move the `GridLayout` -->
        <!-- outside of the ConstraintLayout. -->
        app:layout_constraintTop_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>