一、概念
網(wǎng)格展示控件。
二、使用
1.基本屬性
android:numColumns="auto_fit" 列數(shù)設(shè)置為自動(dòng)
android:columnWidth="90dp" 每列的寬度,也就是Item的寬度
android:stretchMode="columnWidth" 縮放與列寬大小同步
android:verticalSpacing="10dp" 垂直邊距
android:horizontalSpacing="10dp" 水平邊距
2.例子
//activity_grid_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:horizontalSpacing="6dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="6dp"></GridView>
</LinearLayout>
//item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="6dp"
android:text="@string/app_name"
android:textColor="#FFF" />
</LinearLayout>
//Activity
public class GridViewActivity extends AppCompatActivity {
private GridView gridView;
private List<Map<String, Object>> data_list;
private SimpleAdapter adapter;
private int[] icons = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher
, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher
, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher
, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher
, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
private String[] text = {"111", "222", "333", "444", "555", "666", "777", "888", "999", "000", "123", "234", "345", "456", "555"};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_view);
gridView = (GridView) findViewById(R.id.gridView);
data_list = new ArrayList<Map<String, Object>>();
getData();
//加載適配器
String[] form = {"image", "text"};
int[] to = {R.id.image, R.id.text};
adapter = new SimpleAdapter(this, data_list, R.layout.item, form, to);
gridView.setAdapter(adapter);
//監(jiān)聽item每一項(xiàng)
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(GridViewActivity.this, "你點(diǎn)擊了第" + i, Toast.LENGTH_SHORT).show();
}
});
}
//準(zhǔn)備數(shù)據(jù)源
public List<Map<String, Object>> getData() {
for (int i = 0; i < icons.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", icons[i]);
map.put("text", text[i]);
data_list.add(map);
}
return data_list;
}
}