0

我的问题在于 gridview 中的图像是 200x500,但每个网格区域是 500x500(这些示例不准确)。我有一个 2x4 网格,它是静态的,不会改变。我可以通过设置 android:verticalspacing 来更改水平间距,但这会在不同尺寸的屏幕上导致各种问题。最好我只想缩小每个网格以匹配图像并使其在屏幕尺寸上保持动态。下面是我的 Gridview 适配器和布局 xml

public class MainAdapter extends BaseAdapter {
    private Context mContext;

    // Keep all Images in array
    public Integer[] mThumbIds = {
            R.drawable.install, R.drawable.wipe,
            R.drawable.backup, R.drawable.restore,
            R.drawable.mount, R.drawable.settings,
            R.drawable.advanced, R.drawable.reboot
    };

    // Constructor
    public MainAdapter(Context c){
        mContext = c;
    }

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

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        return imageView;
    }

}

XML:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <LinearLayout
        android:id="@+id/bottom_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <com.google.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:id="@+id/adView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        ads:adSize="BANNER"
        ads:adUnitId=""
    />
        </LinearLayout>


    <GridView 
        android:id="@+id/grid_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="2"
        android:columnWidth="90dp"
        android:horizontalSpacing="2dp"
        android:verticalSpacing="10dp"
        android:gravity="center"
        android:stretchMode="columnWidth"
        android:layout_above="@id/bottom_view" >  

    </GridView>
    </RelativeLayout>
4

0 回答 0