Learning content from
Android Layout Basics and Examples: Android nine Gongge layout for everyone
,
Similar to the nine Gongge above is the picture, below is the text
Here is the "GridView" table layout, let me tell you:
First of all, please understand the concept of "iterative display", this is similar to the layout of nesting, we are in a large layout of repeating the layout of some of the same small layout,
Those repetitions are small controls made up of pictures and text, images above, text below, and then we just need to iterate over the widgets into the main container.
First look at the layout of the main container
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<GridView8Android:id="@+id/gridview"9Android:layout_width="fill_parent"Tenandroid:layout_height="fill_parent" Oneandroid:numcolumns="Auto_fit" AAndroid:columnwidth="70DP" -Android:stretchmode="ColumnWidth" -android:gravity="Center"> the</GridView> -</LinearLayout>
Describe some of the properties inside:
Android:numcolumns= "Auto_fit", the GridView column number is set to Automatic
Android:columnwidth= "90DP", width of each column, which is the width of item
Android:stretchmode= "ColumnWidth", Zoom and column size synchronization
The property that needs attention here is ColumnWidth, which specifies the width of the column, a column object that corresponds to a "repeatable subkey," which is the part of our picture item and the text below the image. If you do not specify this width, the default is that each row (the displayed row, the interface) only displays a "repeatable subkey", and when the width is specified, this article is specified as 90DP, and if the actual row size of each row is greater than 90, he will continue to place the next "repeatable subkey" in the bank. This renders a row that shows multiple subkeys. The NumColumns property, which specifies an auto-populated value, indicates that the row is automatically populated.
Then there is a layout, and a small layout that repeats the display
1<?xml version="1.0"encoding="Utf-8"?>2<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"3Android:layout_width="fill_parent"4android:layout_height="wrap_content"5>6<ImageView7Android:layout_width="wrap_content"8Android:id="@+id/itemimage"9android:layout_height="wrap_content"TenAndroid:layout_centerhorizontal="true"/> One<TextView AAndroid:layout_width="wrap_content" -android:layout_height="wrap_content" -android:layout_below="@+id/itemimage" theAndroid:id="@+id/itemtext" -Android:layout_centerhorizontal="true" -/> -</RelativeLayout>
A relative layout is used here, and the attribute android:layout_below= "@+id/itemimage" in TextView indicates that the text is below the picture.
Build ArrayList as the data source, build Simpleadapter as the data adapter, and specify the adapter object for the GridView.
Data structures in Java are used: HashMap
1 Package jiabin.activity;2 3 import java.util.ArrayList;4 import Java.util.HashMap;5 6 import android.app.Activity;7 import Android.os.Bundle;8 import Android.view.View;9 import Android.widget.AdapterView;Ten import Android.widget.AdapterView.OnItemClickListener; One import Android.widget.GridView; A import Android.widget.SimpleAdapter; - import Android.widget.Toast; - the Public classLayout_gridview extends Activity { - /** Called when the activity is first created.*/ - @Override - Public voidonCreate (Bundle savedinstancestate) { + super.oncreate (savedinstancestate); - Setcontentview (r.layout.main); +GridView GridView =(GridView) Findviewbyid (R.id.gridview); AarraylistNewArraylist(); at for(inti =1; I <Ten; i++) - { -hashmap<string, object> map =NewHashmap<string, object>(); -Map.put ("Itemimage", r.drawable.i1); -Map.put ("Itemtext",""+i); - meumlist.add (map); in } -Simpleadapter Saitem =NewSimpleadapter ( This, toMeumlist,//Data Source +R.layout.item,//XML Implementation - Newstring[]{"Itemimage","Itemtext"},//key corresponding to map the New int[]{r.id.itemimage,r.id.itemtext}];//ID corresponding to r * $ //Add Item to GridPanax Notoginseng Gridview.setadapter (saitem); - //Add a Click event the Gridview.setonitemclicklistener ( + NewOnitemclicklistener () A { the Public voidOnitemclick (adapterview<?> arg0, View arg1,intArg2,Longarg3) + { - intindex=arg2+1;//The ID is starting from 0, so it takes +1 $Toast.maketext (Getapplicationcontext (),"you pressed the option:"+index,0). Show (); $ //toast is used to display some help/hints to the user - } - } the ); - }Wuyi}
:
The above is all showing the same picture
If you want to display different pictures and text
You can put the picture, the text in the array
1 int[] Itemimage =New int[]{r.drawable.image_people_shu_zhangfei,r.drawable.image_people_shu_zhugeliang,r.drawable.image_people_shu _liubei,r.drawable.image_people_shu_huatuo,r.drawable.image_people_shu_guanyu}; Put pictures7string[] ItemName =Newstring[]{"Zhang Fei","Zhuge Liang","Liu Bei","Hua Tuo","Guan Yu"}; Put text
for (int i=0;i<6;i++)
{
hashmap<string, object> map = new hashmap<string, object> ();
Map.put ("Itemimage", Itemimage[i]);
Map.put ("Itemtext", "" +itemname[i]);
Meumlist.add (map);
}
You can implement different pictures and text.
Android Development _ Nine Gongge layout