我的 xml 文件中有一个 gridLayout,其中填充了一些 ImageView ,我想以编程方式创建该 gridlayout 的副本
.xml 文件
<GridLayout
android:id="@+id/gridLayoutBucket"
android:layout_width="0dp"
android:layout_height="0dp"
android:columnCount="3"
android:padding="24dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="24dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/textTimerBucket"
app:layout_constraintBottom_toTopOf="@id/gridLayoutBucketBucket"
android:rowCount="2">
<ImageView
android:id="@+id/imageView1Bucket"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:contentDescription="@string/imageDesc"
android:layout_margin="4dp" />
<ImageView
android:id="@+id/imageView2Bucket"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:contentDescription="@string/imageDesc"
android:layout_margin="4dp" />
<ImageView
android:id="@+id/imageView3Bucket"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:contentDescription="@string/imageDesc"
android:layout_margin="4dp" />
<ImageView
android:id="@+id/imageView4Bucket"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:contentDescription="@string/imageDesc"
android:layout_margin="4dp" />
<ImageView
android:id="@+id/imageView5Bucket"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:contentDescription="@string/imageDesc"
android:layout_margin="4dp" />
<ImageView
android:id="@+id/imageView6Bucket"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:contentDescription="@string/imageDesc"
android:layout_margin="4dp" />
</GridLayout>
我尝试重新创建网格布局,它非常接近,但第二条车道裁剪了我的 ImageView 的一些空间 我创建一个 gridLayout 然后设置适当的行和列,此外我创建一个 rowSpec 和一个 colSpec 然后我用 ImageView 填充我的网格布局,给它们一些布局参数,如 width height , rowSpec 和 ColSpec
GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayoutBucket);
gridLayout.removeAllViews();
gridLayout.setColumnCount(3);
gridLayout.setRowCount(2);
int idsetter = 0;
for(int i=0; i<gridLayout.getRowCount(); i++)
{
GridLayout.Spec rowSpec = GridLayout.spec(i, 1,GridLayout.FILL,1);
for(int j=0;j<gridLayout.getColumnCount();j++)
{
GridLayout.Spec colSpec = GridLayout.spec(j,1,GridLayout.FILL,1);
ImageView imageView = new ImageView(this);
imageView.setId(idsetter);
GridLayout.LayoutParams myGLP = new GridLayout.LayoutParams();
// myGLP.rowSpec = rowSpec;
// myGLP.columnSpec = colSpec;
GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
layoutParams.width = 0;
layoutParams.height = 0;
layoutParams.rowSpec = rowSpec;
layoutParams.columnSpec = colSpec;
imageView.setLayoutParams(layoutParams);
gridLayout.addView(imageView, myGLP );
}
}
最佳答案
您的问题在于 XML 格式,尤其是网格上的 GridLayout
padding
。通过分配 android:layout_width="0dp"
android:layout_height="0dp"
,我假设您在同一个 XML 上有另一个具有这些宽度和高度的 View 。通过这样做,您将告诉这 2 个 View 共享剩余的屏幕。现在没关系,但通过在 GridLayout 上添加填充,您可以使 View 向每个方向“拉伸(stretch)”24dp,从而使另一个 View 与 GridLayout 重叠,从而导致您在第二个中描述的裁剪排。
尝试删除 GridLayout
上的 padding
,我想你会被设置的。
关于android - 使用 .xml 文件以编程方式生成相同的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57763798/