Android Studio 3.1,Java 1.8,Gradle 4.1
我使用GridLayout。 一切正常。 但是我需要在列和行之间的空间之间设置空间(例如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>
活动:我以编程方式添加行:
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);
}
答案 0 :(得分:1)
在xml GridLayout中,添加:
app:useDefaultMargins="true"
您将获得列和行之间的距离。
答案 1 :(得分:0)
我找到了解决方案:
O活动:
git for-each-ref --help
答案 2 :(得分:0)
这就是我实现它的方式,希望这对某人有帮助;
<GridLayout
android:orientation="horizontal"
android:columnCount="3"
android:layout_marginEnd="-10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:background="@color/colorBlack"
android:textColor="@color/colorWhite"
android:text="1"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:layout_columnWeight="1"
android:layout_height="wrap_content"
android:padding="15dp" />
<TextView
android:background="@color/colorBlack"
android:textColor="@color/colorWhite"
android:text="2"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:layout_columnWeight="1"
android:layout_height="wrap_content"
android:padding="15dp" />
<TextView
android:background="@color/colorBlack"
android:textColor="@color/colorWhite"
android:text="3"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:layout_columnWeight="1"
android:layout_height="wrap_content"
android:padding="15dp" />
<TextView
android:background="@color/colorBlack"
android:textColor="@color/colorWhite"
android:text="4"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:layout_columnWeight="1"
android:layout_height="wrap_content"
android:padding="15dp" />
</GridLayout>
基本上,这是在每个项目上设置边距(10dp)以在GridLayout中将它们间隔开,然后通过使用负边距(-10dp)来补偿额外的宽度来移动GridLayout。得到以下结果;