使用带有GridView的ImageView设置行和列中显示的图像数

时间:2013-04-20 06:37:48

标签: android android-imageview android-gridview

我想使用ImageView类在GridView布局中布局8x8网格图像,实际上只是40x40px图标。

我尝试过使用ImageView类的 setLayoutParams setScaleType 方法,但无法实现所需的效果。这就是我所拥有的,但我正在尝试只有一个3x3网格的小图标,直到我掌握它为止。

主要活力:

package com.topherwilso.ropasci;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.GridView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GridView gv = (GridView) findViewById(R.id.gridview);
        gv.setAdapter(new ImageAdapter(getApplicationContext()));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

我的ImageAdapter课程:

package com.topherwilso.ropasci;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class ImageAdapter extends BaseAdapter {

    int[] images = {
            R.drawable.one, R.drawable.two,
            R.drawable.three, R.drawable.four,
            R.drawable.five, R.drawable.six,
            R.drawable.seven, R.drawable.eight,
            R.drawable.nine
    };
    private Context context;

    public ImageAdapter(Context applicationContext){
        context=applicationContext;
    }

    @Override
    public int getCount() {
        //Number of data elements to be displayed
        return images.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView iv;
        if(convertView != null){
            iv = (ImageView) convertView;
        }
        else{
            iv = new ImageView(context);
            iv.setLayoutParams(new GridView.LayoutParams(120, 120));
            iv.setScaleType(ScaleType.CENTER);
            iv.setPadding(0, 0, 0, 0);
        }
        iv.setImageResource(images[position]);
        return iv;
    }

}

这就是现在的样子,

What I have now

这就是我想要的,

enter image description here

1 个答案:

答案 0 :(得分:3)

尝试以下

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gridview"
android:padding="40dp"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:columnWidth="0dp"
android:numColumns="3" // set the number of columns to 3
android:verticalSpacing="20dp"
android:horizontalSpacing="20dp" 
android:gravity="center"
/>

http://developer.android.com/guide/topics/ui/layout/gridview.html

http://developer.android.com/reference/android/widget/GridView.html

无法指定行数。如果上述方法不起作用,您可以使用TableLayout而不是gridview。在您的情况下,您有9个图标,因此将列号设置为3应该可行。

对于TableLayout

http://developer.android.com/reference/android/widget/TableLayout.html