Removing the extra padding in a GridView in android

前端 未结 5 745
心在旅途
心在旅途 2020-12-15 22:21

I want to remove the extra padding that appears in a grid view. I have images of the size 128*128 which will be occupying cells in the grid. But somehow there is an extra sp

5条回答
  •  死守一世寂寞
    2020-12-15 22:41

    I used a variation of Shawn's solution.. it looks nice on the Emulator.

    1) Decide on the # of columns, I chose 4

    2) Set the Column Width

    float xdpi = this.getResources().getDisplayMetrics().xdpi;
    int mKeyHeight = (int) ( xdpi/4 );
    
    GridView gridView = (GridView) findViewById(R.id.gridview); 
    gridView.setColumnWidth( mKeyHeight );// same Height & Width
    

    3) Setup the image in your adapter's getView method

    imageView = new ImageView( mContext );
    // (xdpi-4) is for internal padding
    imageView.setLayoutParams(new GridView.LayoutParams( (int) (xdpi-4)/2, (int) (xdpi-4)/2));
    imageView.setScaleType( ImageView.ScaleType.CENTER_CROP );
    imageView.setPadding(1, 1, 1, 1);
    

    4) Layout

    
        
    
    

    That's it.

提交回复
热议问题