2013-03-03 132 views
2

圖片我想填充我整個GridView,沒有照片,沒有任何填充之間的所有空間...完全填充的GridView有沒有填充

我幾乎沒有,但有一個小bug,因爲我可以滾動沒有照片...這就像我滾動,有一個完整的白頁...不明白。

這是我onCreate()方法在活動類:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

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

    gridview.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View v, int position, 
       long id) { 
      Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

這是我ImageAdapter課,getView()方法和一些重要的屬性:

private Context mContext; 
private GridView grid; 

public ImageAdapter(Context c, GridView gridview) { 
    mContext = c; 
    this.grid = gridview; 
} 

// references to our images 
private Integer[] mThumbIds = { 
    R.drawable.sample_2, R.drawable.sample_3, 
    R.drawable.sample_4, R.drawable.sample_5, 
    R.drawable.sample_6, R.drawable.sample_7, 
    R.drawable.sample_0, R.drawable.sample_1, 
    R.drawable.sample_2, R.drawable.sample_3, 
    R.drawable.sample_4, R.drawable.sample_5, 
    R.drawable.sample_6, R.drawable.sample_7, 
    R.drawable.sample_0, R.drawable.sample_1, 
    R.drawable.sample_2, R.drawable.sample_3, 
    R.drawable.sample_4, R.drawable.sample_5, 
    R.drawable.sample_6, R.drawable.sample_7 
}; 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ImageView imageView; 
    if (convertView == null) { // if it's not recycled, initialize some attributes 
     imageView = new ImageView(mContext); 
     imageView.setLayoutParams(new GridView.LayoutParams(
       grid.getWidth()/2, grid.getHeight()/11)); 
     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     imageView.setPadding(0, 0, 0, 0); 
    } else { 
     imageView = (ImageView) convertView; 
    } 

    imageView.setImageResource(mThumbIds[position]); 
    return imageView; 
} 

這是佈局,不太重要的..

<?xml version="1.0" encoding="utf-8"?> 
<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:columnWidth="110dp" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="0dp" 
    android:horizontalSpacing="0dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" /> 

回答

4

問題m AY是在這條線:

imageView.setLayoutParams (new GridView.LayoutParams (grid.getWidth()/2, grid.getHeight()/11)); 

嘗試更換該行:

imageView.setLayoutParams(new GridView.LayoutParams (50, 50)); 

這可以幫助您?

+0

是的,謝謝隊友;) – TiagoM 2013-03-31 21:37:49