无法在GridLayout中设置正确的列宽度

时间:2017-12-21 10:53:57

标签: android android-gridlayout

Android Studio 3.1,Java 1.8。 Gradle 4.1

我想以编程方式将项添加到GridLayout。

这里包含GridLayout

的main.xml
<android.support.constraint.ConstraintLayout
                    android:id="@+id/categoriesContainer"
                    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">

                    <GridLayout
                        android:id="@+id/categoriesGridContainer"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/place_holder_color"                        app:layout_constraintEnd_toEndOf="@+id/categoriesContainer"                        app:layout_constraintStart_toStartOf="@+id/categoriesContainer"                        app:layout_constraintTop_toBottomOf="@+id/categoriesContainer" />
                </android.support.constraint.ConstraintLayout>

此处 profile_category_active .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/profileCategoryContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/profile_category_active_bg">

    <ImageView
        android:id="@+id/categoryCheckImageView"
        android:layout_width="10dp"
        android:layout_height="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_profile_check" />

    <TextView
        android:id="@+id/categoryNameTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/categoryCheckImageView"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

此处以编程方式添加的活动 profile_category_active .xml

ProfileActivity.java

public class ProfileActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ProfileBinding profileBinding = DataBindingUtil.setContentView(this, R.layout.profile);
        init();
    }

    private void init() {
          GridLayout categoriesGridContainer = findViewById(R.id.categoriesGridContainer);
        categoriesGridContainer.setColumnCount(columnCount);
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        for (int index = 0; index < 10; index++) {
            View profileCategoryActive = inflater.inflate(R.layout.profile_category_active, null, false);
            categoriesGridContainer.addView(profileCategoryActive);
            ConstraintLayout profileCategoryContainer = profileCategoryActive.findViewById(R.id.profileCategoryContainer);
            ViewGroup.LayoutParams params = profileCategoryContainer.getLayoutParams();
            TextView categoryNameTextView = profileCategoryActive.findViewById(R.id.categoryNameTextView);
            params.width = (int) categoryItemWidth;
            profileCategoryContainer.setLayoutParams(params);
            categoryNameTextView.setText("Index " + index);
        }        
    }
}

categoryItemWidth 的值为 90dp 。 但是这里结果: gridlayout

正如您所看到的,列的宽度不是 90dp 。也不在textView组件上显示文本。 为什么呢?

1 个答案:

答案 0 :(得分:0)

你可以用这种棘手的方式实现,(只需将边距设置为TextViews)。

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins("left_margin","top_margin","right_margin","bottom_margin");
categoryNameTextView.setLayoutParams(params);