Android 之 GrideView网格视图
程序员文章站
2022-05-16 23:32:52
...
GrideView网格视图:
子布局:
主布局:
程序入口:
子布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridView" android:orientation="vertical" android:gravity="center" android:background="#00000000" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:layout_width="90dp" android:layout_height="100dp" android:id="@+id/image" android:layout_gravity="center_horizontal" android:src="@drawable/home" /> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_marginTop="10dp" android:layout_gravity="center_horizontal" android:text="舒适" /> </LinearLayout>
主布局:
<ImageSwitcher xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/imageSwitch" android:layout_width="match_parent" android:layout_height="wrap_content" />
程序入口:
package com.example.advancedview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.LinearLayout; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; /** * 网格视图 -- 用于显示图片 和 信息 * @author Administrator * */ public class GridViewActivity extends Activity { private GridView grideView ; // 网格视图 // 准备数据源 private int[] images = new int[]{ R.drawable.liyu, R.drawable.jiangkui, R.drawable.liqingzhao, R.drawable.ouyangxiu, R.drawable.qinguan, R.drawable.sushi, R.drawable.wanganshi, R.drawable.xinqiji }; private String[] names = new String[]{ "理喻", "姜夔", "李清照", "欧阳修", "秦观", "苏轼", "王安石", "辛弃疾", }; // 程序入口 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.grideview_layout); // // 获取布局文件中控件的对象 // grideView = (GridView) findViewById(R.id.gridView); // // // M 数据 // List data = this.getData(); // // // c 控制 // SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.gride_item, new String[]{"image","name"}, new int[]{R.id.image,R.id.name}); // // // V 视图 // grideView.setAdapter(adapter); // // // 设置监听器 // grideView.setOnItemClickListener(itemlistener); Intent intent = new Intent(GridViewActivity.this,GalleryActivity.class); startActivity(intent); } // 准备数据源 public List<Map<String,Object>> getData(){ List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); Map map = null; for (int i = 0; i < names.length; i++) { map = new HashMap(); map.put("image", images[i]); map.put("name", names[i]); list.add(map); } return list; } // 网格视图点击子项事件 private OnItemClickListener itemlistener = new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { LinearLayout layout = (LinearLayout) arg0.getAdapter().getView(arg2, null, null); TextView msg = (TextView) layout.findViewById(R.id.name); String m = msg.getText().toString(); Toast.makeText(GridViewActivity.this, ""+arg2, Toast.LENGTH_LONG).show(); } }; }
上一篇: Android 之 PendingIntent用法介绍
下一篇: 大家好, PHP 如何模拟多线程