Android phone defender--nine grid use

Source: Internet
Author: User

This article address: http://www.cnblogs.com/wuyudong/p/5907736.html, reprint please indicate source address.

Implemented with GridView, similar to ListView usage, Number of columns (3 columns)

First add the GridView

    <!--Android:numcolumns The specified number of columns -    <!--android:verticalspacing= "10DP" specifies an internal entry with a vertical spacing of 10DP -    <GridViewAndroid:id= "@+id/gv_home"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:numcolumns= "3"android:verticalspacing= "10DP" >    </GridView>

Data Fill (module name, module picture)

 Packagecom.wuyudong.mobilesafe.activity;ImportCOM.WUYUDONG.MOBILESAFE.R;Importandroid.app.Activity;ImportAndroid.content.ClipData.Item;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.GridView;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView; Public classHomeactivityextendsActivity {PrivateGridView Gv_home; Privatestring[] Mtitlestrs; Private int[] mdrawableids; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_home);        Initui (); //methods for initializing dataInitData (); }    Private voidInitData () {//Prepare data (text (9 groups), picture (9 photos))Mtitlestrs =NewString[] {"Mobile phone anti-theft", "Communication Defender", "Software Management", "Process management", "traffic statistics",                "Mobile Antivirus", "Cache cleanup", "Advanced Tools", "Settings Center" }; Mdrawableids=New int[] {r.drawable.home_safe, R.drawable.home_callmsgsafe, R.drawable.home_apps, r.drawable                . Home_taskmanager, R.drawable.home_netmanager, R.drawable.home_trojan, R.drawable.home_sysoptimize,        R.drawable.home_tools, r.drawable.home_settings}; //Nine Gongge control set data adapter (equivalent to a ListView data adapter)Gv_home.setadapter (NewMyadapter ()); }    Private voidInitui () {gv_home=(GridView) Findviewbyid (r.id.gv_home); }    classMyadapterextends baseadapter {@Override Public intGetCount () {//Total number of entries number of text groups = = Picture count            returnmtitlestrs.length; } @Override PublicObject GetItem (intposition) {            returnMtitlestrs[position]; } @Override Public LongGetitemid (intposition) {            returnposition; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) { View view = view.inflate (Getapplicationcontext (), R.layout.gridview_item,  null ); TextView Tv_title=(TextView) View.findviewbyid (r.id.tv_title); ImageView Iv_icon=(ImageView) View.findviewbyid (R.id.iv_icon);            Tv_title.settext (Mtitlestrs[position]);            Iv_icon.setbackgroundresource (Mdrawableids[position]); returnview; }    }}

Create a new layout file Gridview_item.xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:gravity= "Center"android:orientation= "vertical" >    <ImageViewAndroid:id= "@+id/iv_icon"Android:background= "@drawable/ic_launcher"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" />    <TextViewAndroid:id= "@+id/tv_title"Android:text= "Module title"android:textsize= "18SP"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /></LinearLayout>

The effect after operation is as follows:

Android phone defender--nine grid use

Related Article

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.