GridView auto fit images

前端 未结 3 763
忘了有多久
忘了有多久 2020-12-23 20:52

im trying to get images displayed in a GridView and get the columns automatically set, so far I\'ve had to manually set the number of columns which is not what i want to do

相关标签:
3条回答
  • 2020-12-23 21:07

    A better solution is to measure the width programmatically, so you won't end up with a hard-coded column width:

    Android: How does GridView auto_fit find the number of columns?

    0 讨论(0)
  • 2020-12-23 21:16

    Experiment with the GridView attributes, specifically android:numColumns="auto_fit" and android:stretchMode. The following works for me:

    <GridView 
        android:id="@+id/myGrid"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:padding="10dp"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:columnWidth="60dp"
        android:stretchMode="columnWidth"    
        android:gravity="center"
    />
    
    0 讨论(0)
  • 2020-12-23 21:19

    I'm not sure why someone voted down the answer above me (so I voted it up again): I had to solve a similar problem programatically (with a GridView of ImageView children). Some code removed for clarity:

        int iDisplayWidth = getResources().getDisplayMetrics().widthPixels ;
    
        iImageWidth = iDisplayWidth
            / iNumberOfColumns ; 
        gridview.setColumnWidth( iImageWidth );
        gridview.setStretchMode( GridView.NO_STRETCH ) ;    
        /* wrap_content in the xml file is supposed to do this, but it didn't seem to work */
    

    Best regards,

    Piesia

    0 讨论(0)
提交回复
热议问题