Android instance-Mobile security Defender (iii)-Design the main page UI

Source: Internet
Author: User

First, the goal.

Main interface UI:

Aspect is a Feature List prompt box (with TextView), and below is a list of features (with GridView).

Second, the code implementation.

1. Add components in the main interface layout file (activity_home.xml). The main interface layout file (activity_home.xml) uses a linear layout, the above one textview, according to the UI to set the corresponding properties; The following is the GridView, which sets the number of columns of the component through the Android:numcolumns property. Because the GridView also needs to inflate a single layout file, set the ID for it.

The main interface layout file code is as follows:

1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:orientation= "Vertical" >6 7<TextView8Android:layout_width= "Match_parent"9android:layout_height= "60dip"TenAndroid:background= "#00ffff" Oneandroid:gravity= "Center" Aandroid:text= "Feature List" -Android:textcolor= "#000000" -Android:textsize= "22SP"/> the  -<GridView -android:layout_margintop= "15dip" -Android:verticalspacing= "5dip" +Android:numcolumns= "3" -Android:id= "@+id/home_list" +Android:layout_width= "Match_parent" Aandroid:layout_height= "Match_parent" > at          -</GridView> -  -</LinearLayout>
View Code

2, found in the main interface code to create the GridView object (named Home_list), and in the OnCreate method through Findviewbyid find the GridView component in the layout file. Add an adapter to the GridView object (named Home_list) by using the Setadapter (adapter) method. Since adapter is a ListAdapter interface type, there are many ways to implement it. Therefore, the new Class (Myadapter) inherits its simple class (Baseadapter) and implements GetCount (), getItem (int position), getitemid (int) in the Simple Class (Baseadapter). Position), GetView (int position, View Convertview, ViewGroup Parent) four non-implemented methods.

3. In the new Class (Myadapter).

The ①.getcount () method is to get the item number of the GridView object, which is the same number as the number of text displayed on each item, so create a static string array in the main interface code with the value "mobile phone anti-theft, communication defender, ..." etc. 9 content, And the length of the array as the return value of the GetCount () method. At the same time, in order to find resources convenient, in the main interface code to create a new static int array, for storing the ID of the picture (drawable).

The ②.getview (int position, view Convertview, ViewGroup Parent) method is to get the data that a View object uses to display the specified position (position) in the data group. In this method, the inflate (context context, int resource, ViewGroup root) method of the View object is used to populate the GridView object with an XML type of resource, the parameter context represents the context of the activity where the GridView object resides,int resource represents the ID of the XML type resource, andViewGroup root represents the View object group as the parent component. Generally write null. The inflate () method returns the view type (named view).

③. Create a new XML file (named Home_list_item.xml) under the Layout folder as an XML resource that fills a single item in the GridView object. Its layout is a linear layout, above is the ImageView control and adds an ID to it, the following is the TextView control and adds an ID to it. The purpose of increasing the ID is to find the two controls in the GetView () method and replace the contents of them according to position. For ease of adjustment and presentation, you can set default resources for them. Finally adjust the length, width, background and other properties.

The XML resource (that is, the Home_list_item.xml) code used to populate a single item in the GridView object:

1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:orientation= "Vertical" >6 7<ImageView8Android:id= "@+id/home_list_item_iv"9Android:layout_width= "80dip"Tenandroid:layout_height= "80dip" Oneandroid:layout_gravity= "Center" AAndroid:src= "@drawable/A"/> -  -<TextView theAndroid:id= "@+id/home_list_item_tv" -Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" -android:layout_gravity= "Center" +android:text= "mobile phone anti-theft" -Android:textsize= "20sp"/> +  A</LinearLayout>
View Code

④. Again, in the GetView () method of the new Class (Myadapter) of the main interface code, the view type object returned by the inflate () method is located by the View.findviewbyid () method to find the new XML file (home_list_ ImageView objects and TextView objects in Item.xml, and replaces the contents of them with Setimageresource (int resId) and settext (Charsequence text) methods respectively. In both methods, the ID array of the string array and the drawable can be populated with each item in the GridView object by using the position (position) parameter in the GetView () method. Finally, return to the View object (view).

New Class (Myadapter) code:

1 Private classMyadapterextendsbaseadapter{2 3 @Override4          Public intGetCount () {5             returnnames.length;6         }7         8 @Override9          PublicView GetView (intposition, View Convertview, ViewGroup parent) {TenView view = View.inflate (homeactivity. This, R.layout.home_list_item,NULL);  OneImageView Home_list_iv =(ImageView) View.findviewbyid (R.ID.HOME_LIST_ITEM_IV); A Home_list_iv.setimageresource (drawableid[position]); -TextView HOME_LIST_TV =(TextView) View.findviewbyid (R.ID.HOME_LIST_ITEM_TV); - Home_list_tv.settext (names[position]); the             returnview; -         } -  - @Override +          PublicObject GetItem (intarg0) { -              +             return NULL; A         } at  - @Override -          Public LongGetitemid (intposition) { -              -             return0; -         } in                  -}
View Code

4. New A Myadapter class in the main interface code and bind the adapter to the GridView object through the Setadapter (ListAdapter adapter) method of the GridView object (home_list).

Main interface Code:

1  PackageCom.example.mobilesafe;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.view.View;6 ImportAndroid.view.ViewGroup;7 ImportAndroid.widget.BaseAdapter;8 ImportAndroid.widget.GridView;9 ImportAndroid.widget.ImageView;Ten ImportAndroid.widget.TextView; One  A  Public classHomeactivityextendsActivity { -     PrivateGridView home_list; -     PrivateMyadapter Myadapter; the      -     Private Staticstring[] names = { -"Mobile phone anti-theft", "Communication Defender", "Software Management", -"Process Management", "traffic statistics", "mobile antivirus", +"Cache cleanup", "Advanced Tools", "Settings Center" -     }; +     //create an array of IDs for picture files A     Private Static int[] Drawableid = { at r.drawable.a,r.drawable.b,r.drawable.c, - R.drawable.d,r.drawable.e,r.drawable.f, - r.drawable.g,r.drawable.h,r.drawable.i -     }; -          -          in @Override -     protected voidonCreate (Bundle savedinstancestate) { to         Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_home); -          theHome_list =(GridView) Findviewbyid (r.id.home_list); *Myadapter =NewMyadapter (); $ Home_list.setadapter (myadapter);Panax Notoginseng     } -          the     Private classMyadapterextendsbaseadapter{ +  A @Override the          Public intGetCount () { +             returnnames.length; -         } $          $ @Override -          PublicView GetView (intposition, View Convertview, ViewGroup parent) { -View view = View.inflate (homeactivity. This, R.layout.home_list_item,NULL);  theImageView Home_list_iv =(ImageView) View.findviewbyid (R.ID.HOME_LIST_ITEM_IV); - Home_list_iv.setimageresource (drawableid[position]);WuyiTextView HOME_LIST_TV =(TextView) View.findviewbyid (R.ID.HOME_LIST_ITEM_TV); the Home_list_tv.settext (names[position]); -             returnview; Wu         } -  About @Override $          PublicObject GetItem (intarg0) { -              -             return NULL; -         } A  + @Override the          Public LongGetitemid (intposition) { -              $             return0; the         } the                  the     } the}
View Code

Android instance-Mobile security Defender (iii)-Design the main page UI

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.