Step by step _ Android development course [22] _ GridView (grid view) on the user interface, _ androidgridview
Focus on technology, enjoy life! -- QQ: 804212028.
Link: http://blog.csdn.net/y18334702058/article/details/44624305
- Topic: User Interface GridView (grid view)
-The jiugongge layout is implemented using it.
Use the following code (example) to implement the GridView ):
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <GridView android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:columnWidth="90dp" android:gravity="center" android:padding="30dp" android:numColumns="3" android:stretchMode="columnWidth" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" /></LinearLayout>
Customize the layout of items in the gridview:
Gridview_item.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_centerHorizontal="true"/> <TextView android:id="@+id/str" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_centerHorizontal="true"/></LinearLayout>
MainActivity. java:
Import java. util. arrayList; import java. util. hashMap; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. gridView; import android. widget. simpleAdapter; public class MainActivity extends Activity {private int [] image = {R. drawable. myimage, R. drawable. myimage, R. drawable. Myimage, R. drawable. myimage, R. drawable. myimage, R. drawable. myimage, R. drawable. myimage, R. drawable. myimage}; private String [] str = {"palace layout 1", "palace layout 2", "palace layout 3", "palace Layout 4", "palace layout 5 ", "palace layout 6", "palace layout 7", "palace layout 8" };@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); GridView gridView = (GridView) findViewById (R. id. gridview); ArrayList <HashMap <String, Object> item = new ArrayList <HashMap <String, Object> (); for (int I = 0; I <image. length; I ++) {HashMap <String, Object> map = new HashMap <String, Object> (); map. put ("image", image [I]); map. put ("str", str [I]); item. add (map);} SimpleAdapter simpleAdapter = new SimpleAdapter (this, item, R. layout. gridview_item, new String [] {"image", "str"}, new int [] {R. id. image, R. id. str }); GridView. setAdapter (simpleAdapter); gridView. setOnItemClickListener (new ItemClickListener ();} class ItemClickListener implements OnItemClickListener {@ Override public void onItemClick (AdapterView <?> Arg0, View arg1, int arg2, long arg3) {// TODO Auto-generated method stub // write listener event here }}}
Running result:
Focus on technology, enjoy life! -- QQ: 804212028.
Link: http://blog.csdn.net/y18334702058/article/details/44624305