Android Beginner (--gridview)

Source: Internet
Author: User

This article comes from the legend of the Fat Treasure Road . References must be indicated by source!

The GridView is a multi-control layout like a ListView. The realization of nine squares is the most convenient.

Let's take a look at the picture, not a chicken, isn't it?


Such as. is a way of application. In each of the squares. Put the app icon, and display the app's name below.

Let's take a look at the layout file:

Activity_hello_world.xml

<?

XML version= "1.0" encoding= "Utf-8"?> <gridview xmlns:android= "http://schemas.android.com/apk/res/ Android " android:id=" @+id/gridview " android:layout_width=" fill_parent " android:layout_height=" fill _parent " android:numcolumns=" Auto_fit " android:verticalspacing=" 10DP " android:horizontalspacing=" 10DP " android:columnwidth=" 90DP " android:stretchmode=" columnWidth " android:gravity=" center " / >

Now talk about some of the new properties:

Here DP related knowledge can look at Android length related

Android:numcolumns= "Auto_fit" set to self-adapting to screen width, only can be placed 3
Android:verticalspacing= "10DP" the spacing between the two lines is 10 pixels
android:horizontalspacing= spacing between two columns of "10DP" is 10 pixels
Android:columnwidth= width of each column of "90DP"
Android:stretchmode= "ColumnWidth" scaling is synchronized with the size of the column width
android:gravity= "Center" space is displayed in the middle

Let's take a look at the code:

Helloworldactivity.java

Package Com.fable.helloworld;  Import android.app.Activity;   Import Android.os.Bundle; Import Android.widget.gridview;import Android.widget.SimpleAdapter; Import java.util.*;p ublic class Helloworldactivity extends Activity {@Override public void onCreate (Bundle save        Dinstancestate) {super.oncreate (savedinstancestate);  Setcontentview (R.layout.activity_hello_world);                  Set main layout file GridView GridView = (GridView) Findviewbyid (R.id.gridview); Create data source arraylistcan use the simpleadapter here, using the same method as the ListView. For the use of Simpleadapter in the ListView, you can see here.

In fact, the process is very easy, that is, the source data, conversion Cheng adapter data, and then set the adapter to the GridView.

Like a listview, a layout file is also included in the layout of each item.

My_list_item.xml

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/ Res/android "android:layout_height=" wrap_content "android:paddingbottom=" 4dip "android:layout_width= "Fill_parent" > <imageview android:layout_height= "wrap_content" android:i D= "@+id/itemimage" android:layout_width= "Wrap_content" android:layout_centerhorizontal= "                  True "> </ImageView> <textview android:layout_width=" Wrap_content " android:layout_below= "@+id/itemimage" android:layout_height= "Wrap_content" an droid:text= "TextView01" android:layout_centerhorizontal= "true" android:id= "@+id/itemtext "> </TextView> </RelativeLayout>
Here Itemtext is set below Itemimage, two are vertically centered.








Android Beginner (--gridview)

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.