GridView
android:numColumns="auto_fit" 列数 自动设置
android:columnWidth="90dp" 列宽
android:verticalSpacing="10dp" 行间距
android:horizontalSpacing="10dp" 列间距
android:stretchMode="columnWidth" 缩放方式
package com.example.android_grid_layout;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class MainActivity extends Activity {
private GridView gv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv = (GridView)this.findViewById(R.id.gridView);
ImageAdapter adapter = new ImageAdapter();
gv.setAdapter(adapter);
}
class ImageAdapter extends BaseAdapter{
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_6, R.drawable.sample_7
};
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(MainActivity.this);
// 设置布局参数 大小尺寸
imageView.setLayoutParams(new GridView.LayoutParams(180, 180));
// 图片截取类型
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
// 上下左右内边距
imageView.setPadding(1, 1, 1, 1);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
图片带文字:
package com.example.android_grid_layout;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity2 extends Activity {
private GridView gv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv = (GridView)this.findViewById(R.id.gridView);
ImageAdapter adapter = new ImageAdapter();
gv.setAdapter(adapter);
}
class ImageAdapter extends BaseAdapter{
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1
};
private String[] descs = {
"描述1", "描述2",
"描述3", "描述4",
"描述5", "描述6",
"描述7", "描述8",
"描述9", "描述10",
"描述11", "描述12",
"描述13", "描述14",
"描述13", "描述14"
};
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
TextView textView;
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.grid_item, null);
}
imageView = (ImageView) convertView.findViewById(R.id.imageView1);
textView = (TextView) convertView.findViewById(R.id.textView1);
// 设置布局参数 大小尺寸
// imageView.setLayoutParams(new GridView.LayoutParams(90, 90));
// 图片截取类型
// imageView.setScaleType(ImageView.ScaleType.FIT_XY);
// 上下左右内边距
// imageView.setPadding(1, 1, 1, 1);
imageView.setImageResource(mThumbIds[position]);
textView.setText(descs[position]);
return convertView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}