我正在使用GridLayout(支持)ImageView在我的应用程序中显示s.有3列5行.问题是细胞在GridLayout它们之间自动获得一些空间.我没有为细胞设置任何填充或边距.请参考下图.动态添加所有单元格,以下是添加这些单元格的方法.
获得屏幕宽度和高度:
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
screenWidth = size.x;
screenHeight = size.y;
rowHeight = (int) (screenHeight * 0.2);
Run Code Online (Sandbox Code Playgroud)
将视图添加到GridLayout:
GridLayout.LayoutParams params = new GridLayout.LayoutParams(
getSpec(rowColumn[0]), getSpec(rowColumn[1]));
params.height = rowHeight;
if (rowColumn[1].equalsIgnoreCase("col_full")) {
params.width = (int) (screenWidth);
} else if (rowColumn[1].equalsIgnoreCase("col_two_part")) {
params.width = (int) (screenWidth * 0.6);
} else {
params.width = (int) (screenWidth * 0.3);
}
ImageButton image = (ImageButton) imageHashMap
.get(reOrderedButtonsListCopy.get(i));
image.setLayoutParams(params);
gridLayout.addView(image, params);
Run Code Online (Sandbox Code Playgroud)
XML布局:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.xx.xxx"
android:id="@+id/scrollView1"
android:layout_width="wrap_content" …Run Code Online (Sandbox Code Playgroud) 这个GridLayout在我的应用程序中有很多级别.每个级别都有不同的行数和列数.我认为这个GridLayout是我用来满足我需求的最佳选择.此外,所有这些都需要在运行时以编程方式完成.
我无法理解如何使用GridLayout.spec().我试图遵循这个优秀的例子,但却无法完全掌握它.比方说,我想要一个包含3列和4行的GridLayout.
GridLayout.LayoutParms params1 = new GridLayout.Layout(rowSpec, columnSpec); //what's parameters?
gameplayGridLayout.setColumnCount(3);
gameplayGridLayout.setRowCount(4);
puzzle.addView(gameplayGridLayout, params1);
Run Code Online (Sandbox Code Playgroud)
在我上面的链接示例中,他使用下面的代码来设置"specs".
Spec row1 = GridLayout.spec(0, 2);
Spec row2 = GridLayout.spec(2);
Spec row3 = GridLayout.spec(3);
Spec row4 = GridLayout.spec(4, 2);
Spec col0 = GridLayout.spec(0);
Spec col1 = GridLayout.spec(1);
Spec colspan2 = GridLayout.spec(0, 2);
Run Code Online (Sandbox Code Playgroud)
我也不了解这些变量的参数.我试过阅读文档,但它没有给我任何清晰度.有人可以帮助我使用3x4 GridLayout的示例代码,这也有助于解释Specs是什么?
Android Studio 3.1、Java 1.8、Gradle 4.1
我使用 GridLayout。一切正常。但我需要在列之间和行之间设置空间(例如 10dp)。我怎么能做到这一点?
主文件:
<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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
活动:我以编程方式添加行:
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); …Run Code Online (Sandbox Code Playgroud)