<p>如果是列表(单列多行形式)的使用ListView,如果是多行多列网状形式的优先使用GridView。</p><pre class="brush:xml;toolbar:false"><?xmlversion="1.0"encoding="utf-8"?>
<GridViewxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</GridView></pre><p>GirdView的一些属性:</p><p>android:numColumns="auto_fit" --------列数设置为自动</p><p>android:columnWidth="90dp",----------每列的宽度,也就是Item的宽度</p><p>android:stretchMode="columnWidth"------缩放与列宽大小同步</p><p>android:verticalSpacing="10dp"----------垂直边距</p><p>android:horizontalSpacing="10dp"-------水平边距</p><p>1、准备数据源</p><p>2、新建适配器</p><p>3、加载适配器</p><p>GridView(网格视图)是按照行列的方式来显示内容的,一般用于显示图片,图片等内容,比如实现九宫格图,用GridView是首选,也是最简单的,下面来个实例,</p><p>下载实例>>></p><p>效果图:</p><p><img src="/up_pic/201812/171018407388.jpg" title="171018407388.jpg" alt="1.jpg"/></p><p>MainActivity.java</p><pre class="brush:java;toolbar:false">packagecom.example.testgridview;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.widget.GridView;
importandroid.widget.SimpleAdapter;
publicclassMainActivityextendsActivity{
privateGridViewgview;
privateList<Map<String,Object>>data_list;
privateSimpleAdaptersim_adapter;
//图片封装为一个数组
privateint[]icon={R.drawable.address_book,R.drawable.calendar,
R.drawable.camera,R.drawable.clock,R.drawable.games_control,
R.drawable.messenger,R.drawable.ringtone,R.drawable.settings,
R.drawable.speech_balloon,R.drawable.weather,R.drawable.world,
R.drawable.youtube};
privateString[]iconName={"通讯录","日历","照相机","时钟","游戏","短信","铃声",
"设置","语音","天气","浏览器","视频"};
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
gview=(GridView)findViewById(R.id.gview);
//新建List
data_list=newArrayList<Map<String,Object>>();
//获取数据
getData();
//新建适配器
String[]from={"image","text"};
int[]to={R.id.image,R.id.text};
sim_adapter=newSimpleAdapter(this,data_list,R.layout.item,from,to);
//配置适配器
gview.setAdapter(sim_adapter);
}
publicList<Map<String,Object>>getData(){
//cion和iconName的长度是相同的,这里任选其一都可以
for(inti=0;i<icon.length;i++){
Map<String,Object>map=newHashMap<String,Object>();
map.put("image",icon[i]);
map.put("text",iconName[i]);
data_list.add(map);
}
returndata_list;
}
}</pre><p>test.xml</p><pre class="brush:xml;toolbar:false"><?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000"
>
<GridView
android:id="@+id/gview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:columnWidth="80dp"
android:stretchMode="columnWidth"
></GridView>
</LinearLayout></pre><p>item.xml</p><pre class="brush:xml;toolbar:false"><?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:padding="10dp"
>
<ImageView
android:src="@drawable/ic_launcher"
android:id="@+id/image"
android:layout_width="60dp"
android:layout_height="60dp"
/>
<TextView
android:id="@+id/text"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="文字"
/>
</LinearLayout></pre><p></p><p>监听接口: onItemClickListener</p>