android - 网格布局。如何设置列之间的空间?

标签 android android-gridlayout

Android Studio 3.1、Java 1.8、Gradle 4.1

我使用网格布局。
一切正常。
但我需要设置列之间的空间(例如 10dp)和行之间的空间。
我怎么能做到这一点?

main.xml :

            <GridLayout
                android:id="@+id/categoriesContainer"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:verticalSpacing="10dp"
                app:layout_constraintEnd_toEndOf="@+id/birthDateContainer"
                app:layout_constraintStart_toStartOf="@+id/birthDateContainer"
                app:layout_constraintTop_toBottomOf="@+id/birthDateContainer">


            </GridLayout>

profile_category_active.xml
<?xml version="1.0" encoding="utf-8"?>
<layout 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.support.constraint.ConstraintLayout
        android:id="@+id/profileCategoryContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">       


        <TextView
            android:id="@+id/categoryNameTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginEnd="30dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="30dp"
            android:layout_marginStart="5dp"
            android:layout_marginTop="5dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="TextView"                />
    </android.support.constraint.ConstraintLayout>
</layout>

Activity :我以编程方式添加行:
 GridLayout gridLayout = findViewById(R.id.categoriesContainer);
    gridLayout.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 profileCategoryContaineParams = profileCategoryContainer.getLayoutParams();
            profileCategoryContaineParams.width = (int) AndroidUtil.dpToPx(this, categoryItemWidth);
            profileCategoryContainer.setLayoutParams(profileCategoryContaineParams);
            TextView categoryNameTextView = profileCategoryActive.findViewById(R.id.categoryNameTextView);
            categoryNameTextView.setText("Ind " + profileCategoryContaineParams.width);
}

最佳答案

在 xml GridLayout 中,添加:

android:useDefaultMargins="true"

你得到列和行之间的距离。

关于android - 网格布局。如何设置列之间的空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47920094/

相关文章:

android - Android 布局 Activity 中的光标启动问题

android - 如何让 C2DM 推送通知与启用的 WiFi 一起工作?

android - 具有不同单元格大小、pinterest 样式的 gridView

android - 低于21的API如何在GridLayout中使用布局权重

java - 根据 ArrayList<HashMap<String, String>> 中的值更改 TextView 的颜色

android - Cordova/Phonegap 不在模拟器中启动应用程序

Android/FireStore 查询并返回自定义对象

android - GridLayout 的 rowOrderPreserved 和 columnOrderPreserved

java - 在运行时在 android 中更改 GridLayout 行/列数

Android onClickListener 没有 xml