Controls related to Adapter in android ---- GridView and androidadapter controls

Source: Internet
Author: User

Controls related to Adapter in android ---- GridView and androidadapter controls

GridView (grid view)

1. The GridView (grid view) has many controls, which are the same as listView in many places. However, the GridView can display multiple columns, while the listView can only display one column, I personally think this is the biggest difference. Common attributes:

Android: columnWidth: Set the column width

Android: gravity: Alignment of components

Android: horizontalSpacing: The spacing of each cell in the horizontal direction

Android: verticalSpacing: spacing of each cell in the vertical direction

Android: numColumns: set the number of columns. The default value is 1.

Android: stretchMode: sets the stretch mode. Optional values: none. spacingWidth: just stretch the table element's own spacingWidthUniform to stretch both the element spacing and the gap between them

Ii. Use instances

<?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">    <GridView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:numColumns="2"        android:id="@+id/gv">    </GridView></LinearLayout>
<? 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 = "horizontal"> <ImageView android: id = "@ + id/image1" android: layout_width = "100dp" android: layout_height = "100dp" android: src = "@ mipmap/ic_launcher"/> <TextView android: id = "@ + id/text1" android: layout_width = "match_parent" android: layout_height = "100dp" android: gravity = "center" android: text = "Haha" android: textSize = "30sp"/> </LinearLayout>

Java files

Package com. example. test3; import android. app. activity; import android. OS. bundle; import android. widget. gridView; import android. widget. simpleAdapter; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; public class MainActivity extends Activity {private GridView; private List <Map <String, Object> list; private SimpleAdapter adapter; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); gridView = (GridView) findViewById (R. id. gv); list = new ArrayList <> (); for (int I = 0; I <10; I ++) {Map <String, object> map = new HashMap <> (); map. put ("image", R. mipmap. ic_launcher); map. put ("content", "Haha" + I); list. add (map);} adapter = new SimpleAdapter (MainActivity. this, list, R. layout. item, new String [] {"image", "content"}, new int [] {R. id. image1, R. id. text1}); gridView. setAdapter (adapter );}}

 

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.