从零开始学android<GridView网格视图.二十八.>
1
|
public GridView(Context context)
|
构造
|
|
创建GridView对象
|
2
|
public void setStretchMode(int stretchMode)
|
普通
|
android:stretchMode
|
缩放模式
|
3
|
public void setVerticalSpacing(int verticalSpacing)
|
普通
|
android:verticalSpacing
|
设置垂直间距
|
4
|
public void setHorizontalSpacing(int horizontalSpacing)
|
普通
|
android:horizontalSpacing
|
设置水平间距
|
5
|
public void setNumColumns(int numColumns)
|
普通
|
android:numColumns
|
设置每列显示的数据量,如果设置为auto_fit则表示自动设置
|
6
|
public void setSelection(int position)
|
普通
|
|
设置默认选中项
|
7
|
public void setGravity(int gravity)
|
普通
|
android:gravity
|
设置对齐模式,由Gravity类指定
|
8
|
public void setAdapter(ListAdapter adapter)
|
普通
|
|
设置显示图片集
|
<span style="font-size:18px;"><RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <GridView android:id="@+id/gridview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:stretchMode="columnWidth" android:numColumns="4" /> </RelativeLayout> </span>
JAVA文件
<span style="font-size:18px;">package com.example.gridview; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; public class MainActivity extends Activity { private GridView gridView;// 初始化GridView private int images[] = { R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6, R.drawable.a7, R.drawable.a8, R.drawable.a9, R.drawable.a10, R.drawable.a11, R.drawable.a12, R.drawable.a13, R.drawable.a14, R.drawable.a15, R.drawable.a16 };// 图片数据 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获得GridView对象 gridView = (GridView) this.findViewById(R.id.gridview); // 设置Adpter gridView.setAdapter(new MyAdapter()); // 设置监听事件 gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { // TODO Auto-generated method stub // 为dialog设置view空间 ImageView image = new ImageView(MainActivity.this); // 设置图片信息 image.setImageResource(images[position]); // 设置图片的额对齐方式 image.setScaleType(ImageView.ScaleType.CENTER); // 设置图片的大小 image.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); // 创建对话框 AlertDialog.Builder builder = new AlertDialog.Builder( MainActivity.this) .setTitle("查看图片") .setView(image) .setIcon(android.R.drawable.btn_star_big_on) .setNegativeButton("关闭", new OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }); //创建对话框 builder.create(); //显示对话框 builder.show(); } }); } class MyAdapter extends BaseAdapter { @Override public int getCount() { // TODO Auto-generated method stub return images.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View myView, ViewGroup parent) { // TODO Auto-generated method stub // 返回imageView对象 ImageView imageView = new ImageView(MainActivity.this); imageView.setImageResource(images[position]); imageView.setScaleType(ImageView.ScaleType.CENTER); return imageView; } } } </span>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。