Activity_ui6.xml
<?XML version= "1.0" encoding= "Utf-8"?><GridViewxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:id= "@+id/gv_1"Android:numcolumns= "Auto_fit"android:verticalspacing= "10DP"android:horizontalspacing= "10DP"Android:columnwidth= "100DP"android:gravity= "Center"Android:stretchmode= "ColumnWidth"> <!--stretchmode: Zoom mode columnWidth: Set according to Lekonde -</GridView>
Uiactivity6.java
PackageCom.hanqi.test5;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.AdapterView;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.GridView;ImportAndroid.widget.ImageView;ImportAndroid.widget.Toast; Public classUIActivity6extendsappcompatactivity {int[] imid; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.LAYOUT.ACTIVITY_UI6); GridView gv_1=(GridView) Findviewbyid (r.id.gv_1); IMiD=New int[]{r.drawable.yuantu,r.drawable.yuantu,r.drawable.yuantu,r.drawable.yuantu,r.drawable.yuantu, R.drawable.yuantu,r.drawable.yuantu,r.drawable.yuantu}; Gv_1.setadapter (NewMybaseadapter ()); Gv_1.setonitemclicklistener (NewAdapterview.onitemclicklistener () {@Override Public voidOnitemclick (adapterview<?> Parent, view view,intPositionLongID) {toast.maketext (UIActivity6. This, "position =" +position, Toast.length_short). Show (); } }); } classMybaseadapterextendsBaseadapter {@Override Public intGetCount () {returnimid.length; } @Override PublicObject GetItem (intposition) { returnImid[position]; } @Override Public LongGetitemid (intposition) { returnposition; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) { //Get Data intImageid =Imid[position]; //Constructing viewsImageView iv_1 =NULL; if(Convertview = =NULL) {iv_1=NewImageView (UIActivity6. This); Iv_1.setlayoutparams (NewGridview.layoutparams (100,100));//Property SettingsIv_1.setscaletype (ImageView.ScaleType.FIT_XY);//fit_xy: Auto Full full } Else{iv_1= (ImageView) Convertview;//Strong Turn} iv_1.setimageresource (Imageid); returniv_1; } }}
as follows (because the picture itself is rectangular, the grid view is scaled to a square, a bit unattractive):
A summary of the grid view:
Android Course---Learn about the GridView grid view