如何为平板电脑和手机屏幕尺寸创建gridview?

时间:2017-02-07 08:47:51

标签: android gridview layout

我在谷歌和本网站上搜索和推荐。但我没有得到正确答案。在我的手机屏幕尺寸中,网格视图是可以的。但对于平板电脑屏幕尺寸,网格视图会发生混乱。

gridview.xml的代码

<GridView
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="8dp"
    android:columnWidth="90dp"
    android:gravity="center"
    android:horizontalSpacing="8dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="8dp">
MainActivity.java的代码

gridView = (GridView) findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(this));

ImageAdapter.java的代码

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(220, 210));
    } else {
        imageView = (ImageView) convertView;
    }
    imageView.setImageResource(imageID[position]);
    return imageView;
}

Phone Screen / Tablet Screen

1 个答案:

答案 0 :(得分:0)

Recycler视图功能更强大。您可以使用GridLayoutManager进行尝试。

点击此处。

https://inducesmile.com/android/android-gridlayoutmanager-with-recyclerview-in-material-design/