Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在GridLayout中将ImageView居中?_Android_Imageview_Android Gridlayout - Fatal编程技术网

Android 如何在GridLayout中将ImageView居中?

Android 如何在GridLayout中将ImageView居中?,android,imageview,android-gridlayout,Android,Imageview,Android Gridlayout,我有一个全屏幕网格布局,有8行7个ImageView。 我需要所有的行居中,它们之间没有空格(现在我有作为我上传的图像的结果)。空间必须仅在顶部和底部。我已经试着在互联网上找到解决方案,但还没有找到 XML: 我尝试将所有ImageView高度设置为宽度,但我的代码无效: void setGrid(){ //setting the cell height as the cell widht int imagWidth = cell[0][0].getLayoutParams(

我有一个全屏幕网格布局,有8行7个ImageView。 我需要所有的行居中,它们之间没有空格(现在我有作为我上传的图像的结果)。空间必须仅在顶部和底部。我已经试着在互联网上找到解决方案,但还没有找到

XML:


我尝试将所有ImageView高度设置为宽度,但我的代码无效:

void setGrid(){
    //setting the cell height as the cell widht
    int imagWidth = cell[0][0].getLayoutParams().width;
    for(int r=0;r<rowCount;r++)
        for(int c=0;c<columnCount;c++) {
            cell[r][c].getLayoutParams().height = imagWidth;
            cell[r][c].requestLayout();
        }
}
void setGrid(){
//将单元格高度设置为单元格宽度
int imagWidth=cell[0][0].getLayoutParams().width;

对于(int r=0;r它可能对您有所帮助,请使用RecyclerView而不是GridLayout

RecyclerView.LayoutManager recyclerViewLayoutManager = new GridLayoutManager(getApplicationContext(), 7); // 7 means how many column you want you can change according to your requirement.
recyclerView.setLayoutManager(recyclerViewLayoutManager);

尝试将RecyclerView与GridLayoutManager一起使用,如下所示:

GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 7);
            YourAdapter adapter = new YourAdapter();
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setAdapter(adapter);

            int spacing = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16,
                    Objects.requireNonNull(getContext()).getResources().getDisplayMetrics());
            recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(@NotNull Rect outRect, @NotNull View view, @NotNull RecyclerView parent, @NotNull RecyclerView.State state) {

                    if (parent.getChildAdapterPosition(view) == Objects.requireNonNull(parent.getAdapter()).getItemCount() - 1) {
                        outRect.bottom = spacing;
                    }
                    if (parent.getChildAdapterPosition(view) == 0) {
                        outRect.top = spacing;
                    }
                }
            });
其他解决方案是: 您可以只将填充添加到RecyclerView的顶部和底部,并将
cliptoppadding
属性设置为
false
,而不是同时向顶部和底部项目添加填充

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:paddingTop="8dp"
    android:paddingBottom="8dp" />


您知道一些事情。我相信您可以使用gridLayout。recyclerview如何?您需要搜索recyclerview和GridLayoutManager。您可以回答一些您已经陈述了希望实现的目标的答案-但是您的代码现在生成了什么?这样我们可以尝试修复它。我上传的图像。如果您指的是我写信来修复它,但什么也没改变(见图)
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:paddingTop="8dp"
    android:paddingBottom="8dp" />