Android的UI组件之GridView

来源:互联网 发布:mac os x10.11 编辑:程序博客网 时间:2024/05/10 09:22

最近打算做一个系统,在进入登陆界面后,显示这个系统的功能,通过学习李刚老师的疯狂android讲义,打算用网格式图GridView,下面是GridView的练习。

先上图:


下面是两个xml代码:

Activity界面代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"     ><GridView     android:id="@+id/grid01"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:horizontalSpacing="pt"    android:verticalSpacing="2pt"    android:numColumns="4"    android:gravity="center"     ></GridView><ImageSwitcher     android:id="@+id/switcher"    android:layout_width="320dp"    android:layout_height="320dp"    android:layout_gravity="center_horizontal"    ></ImageSwitcher></LinearLayout>
为适配器simpleAdapter编写的界面布局cell.xml:

<?xml version="1.0"encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_height="wrap_content"    android:layout_width="fill_parent"    android:gravity="center_horizontal"    android:padding="5pt"    ><ImageView     android:id="@+id/image1"    android:layout_width="50dp"    android:layout_height="50dp"    /><TextView     android:id="@+id/text1"    android:layout_width="50dp"    android:layout_height="50dp"    android:gravity="center_horizontal"    /></LinearLayout>
 GridViewTest.java代码:

package com.example.gridviewtest;import java.util.ArrayList;public class GridViewTest extends Activity {    private static final String TAG="==CrazyIt.org";    String[] names={"查询","修改","注册","消息","工资","功能1","功能2","功能3","功能4","功能5","功能6","功能7",};    int[] imageIds=new int[]{    R.drawable.bomb5,R.drawable.bomb6,R.drawable.bomb7,    R.drawable.bomb8,R.drawable.bomb9, R.drawable.bomb10    , R.drawable.bomb11 , R.drawable.bomb12, R.drawable.bomb13    , R.drawable.bomb14 , R.drawable.bomb15 , R.drawable.bomb16     };@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_grid_view_test);      //创建一个List集合,List集合的元素是Map    List<Map<String, Object>> listItems=new ArrayList<Map<String,Object>>();    for(int i=0;i<imageIds.length;i++){    Map<String, Object> listItem1=new HashMap<String, Object>();    listItem1.put("image", imageIds[i]);    listItem1.put("name", names[i]);    listItems.add(listItem1);        }    //获取显示图片的ImageSwitcher    final ImageSwitcher switcher=(ImageSwitcher)findViewById(R.id.switcher);    //设置图片更换的动画效果    switcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));    switcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));    //为ImageSwitcher设置图片切换的动画效果    switcher.setFactory(new ViewFactory() {public View makeView() {// TODO Auto-generated method stubImageView imageView =new ImageView(GridViewTest.this);imageView.setBackgroundColor(0xff0000);imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));return imageView;}});    //创建一个SimpleAdapter    SimpleAdapter simpleAdapter=new SimpleAdapter(this, listItems, R.layout.cell, new String[]{"image","name"}, new int []{R.id.image1,R.id.text1});    GridView gridView=(GridView)findViewById(R.id.grid01);    //为GridView设置Adapter    gridView.setAdapter(simpleAdapter);    //添加列表项被选中的监听器    gridView.setOnItemSelectedListener(new OnItemSelectedListener() {public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {// 显示被选中的图片switcher.setImageResource(imageIds[arg2%imageIds.length]);}public void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}});    //添加列表项被单击的监听器    gridView.setOnItemClickListener(new OnItemClickListener() {public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {// 显示被单击的图片switcher.setImageResource(imageIds[arg2%imageIds.length]);}});}}
在编写的时候遇到个错误就是:
在代码
for(int i=0;i<imageIds.length;i++){    Map<String, Object> listItem1=new HashMap<String, Object>();    listItem1.put("image", imageIds[i]);    listItem1.put("name", names[i]);    listItems.add(listItem1);        }

中要想把ImageView和TextView同时显示在一起即:文字在图片的下面,应该正如上面的代码一样。自己当时的错误是:

for(int i=0;i<imageIds.length;i++){    Map<String, Object> listItem1=new HashMap<String, Object>();    Map<String, Object> listItem2=new HashMap<String, Object>();    listItem1.put("image", imageIds[i]);    listItem2.put("name", names[i]);    listItems.add(listItem1);    listItems.add(listItem2);        }
对我这个java和android的初学者来说这个错误出现的对学习有很大帮助!故此记录下来。

原创粉丝点击