Android Course---Learn about the GridView grid view

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.