This article illustrates the nine implementation of Android programming. Share to everyone for your reference, specific as follows:
Display nine need to use GridView, there are two ways to display the view in each grid, the first way is to make an XML file, and then the XML file into a view. The second way is to build such a layout in your code, which is implemented in the first way:
Gridview:
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
android:layout_width=" fill_parent "
android:layout_height=" fill_parent "
>
< !--ID gv_all
width is the
10px vertical distance from the parent form numcolums to 3
horizontal controls
is 10px
gridview off the bottom 58px
off the top of 28px
from left 5px to
right 5px
-->
<gridview
android:id= "@+id/gv_all"
android:layout_ height= "Fill_parent"
android:layout_width= "fill_parent"
android:numcolumns= "3"
android: horizontalspacing= "10px"
android:verticalspacing= "10px"
android:layout_marginbottom= "58px"
android:layout_margintop= "28px"
android:layout_marginleft= "5px"
android:layout_marginright= "5px"
></GridView>
</RelativeLayout>
View:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/" Apk/res/android "
android:orientation=" vertical "
android:layout_width=" 90px "
android:layout_height= "90px" >
<imageview
android:layout_width= "64px"
android:layout_height= "64px"
android: Layout_gravity= "Center_horizontal"
android:id= "@+id/main_gv_iv"
/>
<textview
android : layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_gravity= "Center_" Horizontal "
android:textsize=" 16px "
android:textcolor=" #FFF "
android:id=" @+id/main_gv_tv
" />
</LinearLayout>
Class
public class Mainactivity extends activity {public static final String TAG = "mainactivity";
GridView MAINGV;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Full Screen GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN
);
Setcontentview (r.layout.mainactivity);
Gets to the GridView MAINGV = (GridView) This.findviewbyid (R.id.gv_all);
Set the data adapter Maingv.setadapter to the GridView (new Maingridviewadapter (this));
Click event Maingv.setonitemclicklistener (New Mainitemclicklistener ()); Private class Mainitemclicklistener implements onitemclicklistener{/** * @param parent Represents the current GridView * @param view represents the item * @param position the current click of the item in the match * @param ID the item in the current click on which line */public void Onite
Mclick (adapterview<?> Parent, view view, int position, long ID) {switch (position) {case 0: Intent inteNT = new Intent (mainactivity.this,lostprotectedactivity.class);
StartActivity (Intent);
Break
}
}
}
}
Set data adapter:
Complete the GridView data to interface fit public class Maingridviewadapter extends Baseadapter {private static final String TAG = "Maingr
Idviewadapter ";
Private string[] names = {"Mobile burglar", "Communication Guardian", "Software Management", "Task management", "Internet Management", "mobile Antivirus", "System Optimization", "Advanced tools", "Setting Center"}; Private int[] icons = {R.drawable.safe,r.drawable.callmsgsafe,r.drawable.app,r.drawable.taskmanager,
R.drawable.netmanager,r.drawable.trojan,r.drawable.sysoptimize,r.drawable.atools,r.drawable.settings};
private context;
Layoutinflater Infalter;
Public Maingridviewadapter {this.context = context;
Method 1 is obtained through the SERVICE of the system to attempt the filler//infalter = (layoutinflater) context.getsystemservice (Context.layout_inflater_service);
Method 2 obtains the view filler Infalter = layoutinflater.from (context) through Layoutinflater static method;
//Returns how many entries in the GridView are public int GetCount () {return names.length;
//Returns a position corresponding entry public Object getitem (int position) {return position; //Returns a position corresponding ID public long getitemid (int position) {return position; //Return to the view of the corresponding location public view GetView (int position, view Convertview, ViewGroup parent) {LOG.I (TAG, "GetView" + posi
tion);
Converts a layout file into view views = Infalter.inflate (R.layout.mainactivity_item, NULL);
ImageView IV = (ImageView) View.findviewbyid (R.ID.MAIN_GV_IV);
TextView TV = (TextView) View.findviewbyid (R.ID.MAIN_GV_TV);
Set the name and icon of each item Iv.setimageresource (Icons[position]);
Tv.settext (Names[position]);
return view;
}
}
I hope this article will help you with your Android programming.