Android:控件GridView的使用

<p>如果是列表(单列多行形式)的使用ListView,如果是多行多列网状形式的优先使用GridView。</p><pre class="brush:xml;toolbar:false">&lt;?xmlversion=&quot;1.0&quot;encoding=&quot;utf-8&quot;?&gt; &lt;GridViewxmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;match_parent&quot;&gt; &lt;/GridView&gt;</pre><p>GirdView的一些属性:</p><p>android:numColumns=&quot;auto_fit&quot; --------列数设置为自动</p><p>android:columnWidth=&quot;90dp&quot;,----------每列的宽度,也就是Item的宽度</p><p>android:stretchMode=&quot;columnWidth&quot;------缩放与列宽大小同步</p><p>android:verticalSpacing=&quot;10dp&quot;----------垂直边距</p><p>android:horizontalSpacing=&quot;10dp&quot;-------水平边距</p><p>1、准备数据源</p><p>2、新建适配器</p><p>3、加载适配器</p><p>GridView(网格视图)是按照行列的方式来显示内容的,一般用于显示图片,图片等内容,比如实现九宫格图,用GridView是首选,也是最简单的,下面来个实例,</p><p>下载实例&gt;&gt;&gt;</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&lt;Map&lt;String,Object&gt;&gt;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={&quot;通讯录&quot;,&quot;日历&quot;,&quot;照相机&quot;,&quot;时钟&quot;,&quot;游戏&quot;,&quot;短信&quot;,&quot;铃声&quot;, &quot;设置&quot;,&quot;语音&quot;,&quot;天气&quot;,&quot;浏览器&quot;,&quot;视频&quot;}; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.test); gview=(GridView)findViewById(R.id.gview); //新建List data_list=newArrayList&lt;Map&lt;String,Object&gt;&gt;(); //获取数据 getData(); //新建适配器 String[]from={&quot;image&quot;,&quot;text&quot;}; int[]to={R.id.image,R.id.text}; sim_adapter=newSimpleAdapter(this,data_list,R.layout.item,from,to); //配置适配器 gview.setAdapter(sim_adapter); } publicList&lt;Map&lt;String,Object&gt;&gt;getData(){ //cion和iconName的长度是相同的,这里任选其一都可以 for(inti=0;i&lt;icon.length;i++){ Map&lt;String,Object&gt;map=newHashMap&lt;String,Object&gt;(); map.put(&quot;image&quot;,icon[i]); map.put(&quot;text&quot;,iconName[i]); data_list.add(map); } returndata_list; } }</pre><p>test.xml</p><pre class="brush:xml;toolbar:false">&lt;?xmlversion=&quot;1.0&quot;encoding=&quot;utf-8&quot;?&gt; &lt;LinearLayoutxmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;match_parent&quot; android:orientation=&quot;vertical&quot; android:background=&quot;#000&quot; &gt; &lt;GridView android:id=&quot;@+id/gview&quot; android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;wrap_content&quot; android:numColumns=&quot;auto_fit&quot; android:columnWidth=&quot;80dp&quot; android:stretchMode=&quot;columnWidth&quot; &gt;&lt;/GridView&gt; &lt;/LinearLayout&gt;</pre><p>item.xml</p><pre class="brush:xml;toolbar:false">&lt;?xmlversion=&quot;1.0&quot;encoding=&quot;utf-8&quot;?&gt; &lt;LinearLayoutxmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:orientation=&quot;vertical&quot; android:gravity=&quot;center&quot; android:padding=&quot;10dp&quot; &gt; &lt;ImageView android:src=&quot;@drawable/ic_launcher&quot; android:id=&quot;@+id/image&quot; android:layout_width=&quot;60dp&quot; android:layout_height=&quot;60dp&quot; /&gt; &lt;TextView android:id=&quot;@+id/text&quot; android:layout_marginTop=&quot;5dp&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:textColor=&quot;#ffffff&quot; android:text=&quot;文字&quot; /&gt; &lt;/LinearLayout&gt;</pre><p></p><p>监听接口: onItemClickListener</p>
RangeTime:0.007230s
RangeMem:211.59 KB
返回顶部 留言