如何在gridview中动态更改网格大小?

时间:2013-05-17 17:55:07

标签: android android-layout

我的问题在于,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>

0 个答案:

没有答案