将带有ImageViews的GridView调整为不同的屏幕尺寸,Android [英] Adjust GridView with ImageViews to different screen sizes, Android

查看:90
本文介绍了将带有ImageViews的GridView调整为不同的屏幕尺寸,Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人可以在下面提出建议,我将不胜感激.

I'd so appreciate if someone could advise on below.

我的GridView有3列将被ImageViews填充:

My GridView has 3 columns that will be filled with ImageViews:

 <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/grid_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:numColumns="3"
        android:columnWidth="0.3dp"
        android:horizontalSpacing="20dp"
        android:verticalSpacing="20dp"
        android:stretchMode="columnWidth">
    </GridView>

ImageViews添加到网格单元中的适配器:

The Adapter that adds ImageViews into the grid cells:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ImageView imageView = new ImageView(mContext);
    imageView.setImageResource(mThumbIds.get(position));
    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    imageView.setLayoutParams(new GridView.LayoutParams(300, 300));//problem here

    return imageView;
}

我的图片大小均为358x385px.

My images are all of the size 358x385px.

在较大的屏幕上它可以正常工作,但在较小的屏幕上(例如4英寸,800x480)则无法运行. 我不确定如何将LayoutParams设置为屏幕宽度和高度的1/3以形成正方形.

It works fine on bigger screens but fails on smaller ones (for example 4", 800x480). I'm not sure how to set LayoutParams to be like 1/3 of the screen width and the height to form a square.

我不想为不同的屏幕密度添加更多具有适当大小的图像,而是只想调整ImageViews的大小.

I wouldn't like to add more images with appropriate sizes for different screen densities, instead I want to resize ImageViews only.

推荐答案

将此代码用于squre gridview兼容的所有设备.

Follow this code for squre gridview to all device compitible.

步骤:1创建一个扩展了Imageview的类SquareImageView.class.

public class SquareImageView extends ImageView {

public SquareImageView(Context context) {
    super(context);
}

public SquareImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}
}

步骤:2像这样更改适配器.

@Override
public View getView(int position, View convertView, ViewGroup parent) {

SquareImageView imageView = new SquareImageView (mContext);
imageView.setImageResource(mThumbIds.get(position));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

return imageView;
}

还有您的xml

<GridView
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="3"
    android:gravity="center"
    android:padding="3dp"
    android:stretchMode="columnWidth"
    android:horizontalSpacing="2dp"
    android:verticalSpacing="2dp"
    />

并在小屏幕设备上进行测试.它将为您提供帮助

And test in small screen device..It will help you

这篇关于将带有ImageViews的GridView调整为不同的屏幕尺寸,Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆