ImageButtons在GrdView不同的屏幕尺寸

时间:2012-03-28 11:42:07

标签: android android-layout

我正在审核并修复从其他开发人员开发的Android应用程序中的一些问题。

他为正常的屏幕尺寸创建了一个屏幕布局,并在其中插入了GridView

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:verticalSpacing="45dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="120dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:layout_marginTop="30dp"
    android:scrollbars="none"
/>

在代码端图像定义为integer数组。

Integer[] a1 = {                R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,                R.drawable.image5,R.drawable.image6,R.drawable.image6,R.drawable.image7,R.drawable.image8,              R.drawable.image9,R.drawable.image10,R.drawable.image11
        };

带有此代码的整数数组绑定gridview。

GridView gridview = (GridView) findViewById(R.id.gridview);     
gridview.setAdapter(new ImageButtonAdapter(this,infox));

所以这里是ImageButtonAdapter类

public class ImageButtonAdapter extends BaseAdapter {

        private Context mContext;

        public ImageButtonAdapter(Context c, Integer[] imageIds) {
            mContext = c;           
            mThumbIds = imageIds;
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

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

            ImageButton imageButtonx;
            if (convertView == null) {  
                imageButtonx = new ImageButton(mContext);
                imageButtonx.setBackgroundColor(Color.TRANSPARENT);                         
                **imageButtonx.setLayoutParams(new GridView.LayoutParams(160, 160));**
                imageButtonx.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageButtonx.setPadding(5, 5, 5, 5);
                /* if ( position == 10 )
                {
                    imageButtonx.setVisibility(ImageButton.GONE);
                }
                */

            } else {
                imageButtonx = (ImageButton) convertView;
            }

                setMyTag(imageButtonx,mThumbIds[position]);
            imageButtonx.setImageResource(mThumbIds[position]);

            return imageButtonx;
            }
        private Integer[] mThumbIds;
    }

此外,res \ drawable-hdpi文件夹包含167x161像素的高分辨率图像,res \ drawable-mdpi文件夹包含82x72像素的普通分辨率图像。

但是在屏幕较小的设备上,图像会拉伸。我认为粗体代码行会导致这种情况;但我无法解决它。

您有什么建议我如何解决这个问题并支持大型普通和小型屏幕?

2 个答案:

答案 0 :(得分:0)

是的,因为你正在解决这个问题

imageButtonx.setLayoutParams(new GridView.LayoutParams(160, 160))

像这样更改此代码

imageButtonx.setLayoutParams(new GridView.LayoutParams(
                      (int) getResources().getDimension(R.dimen.width),                                                                                   
                      (int) getResources().getDimension(R.dimen.height)));

values-->dimentions.xml文件夹中存储宽度,高度值,如下所示

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <dimen name="width">80dp</dimen>  // your desired values 
   <dimen name="height">80dp</dimen>
</resources> 

HTH ..

答案 1 :(得分:0)

LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

这是我通常做的,希望这会有所帮助。

相关问题