GridView的属性和方法如下:
android:columnWidth-->设置列的宽度-->方法:setColumnwidth(int)
android:graity-->设置对齐方式:-->方法:setGrity(int)
android:horizontalSpacing-->设置各元素之间的水平间距-->方法:setHorizontalSpacing(int)
android:verticalSpacing-->设置各元素之间的垂直间距-->方法:setVerticalSpacing(int)
android:numColumns-->设置列数-->方法:setNumColumns(int)
android:stretchMode-->设置拉伸模式-->方法:setStretchMode(int)
实例如下:
activity_main.xml
<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" android:layout_margin="5dp" tools:context=".MainActivity"> <GridView android:id="@+id/gridView" android:numColumns="4" android:horizontalSpacing="1pt" android:verticalSpacing="1pt" android:layout_width="match_parent" android:layout_height="wrap_content"/> <ImageView android:id="@+id/imageView" android:layout_gravity="center_horizontal" android:layout_width="240px" android:layout_height="240px" /> </LinearLayout>
每一格的局部布局文件:image_call.xml
<?xml version="1.0" encoding="utf-8"?> <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/imageView3" android:layout_width="30pt" android:layout_height="30pt"> </ImageView>
MainActivity.java
package cn.edu.hpu.acm.gridViewTest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.GridView; import android.widget.ImageView; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends AppCompatActivity { //定义属性 private GridView gridView; private ImageView imageView; //定义Image数组 int[] imagesId = { R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6, R.drawable.a7, R.drawable.a8 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //得到控件 gridView = (GridView) findViewById(R.id.gridView); imageView = (ImageView) findViewById(R.id.imageView); //声明List<E>数组,这里的E为Map<String,?> List<Map<String,Object>> listItems = new ArrayList<Map<String, Object>>(); //设置Map<String,?>数组的键值 for (int i=0;i<imagesId.length;i++){ Map<String,Object> listItem = new HashMap<String, Object>(); listItem.put("imageView3",imagesId[i]); //添加到List数组中 listItems.add(listItem); } //设置SimpleAdapter属性 SimpleAdapter simpleAdapter = new SimpleAdapter(this,listItems, R.layout.image_cell,new String[]{"imageView3"}, new int[]{R.id.imageView3}); //设置GridView的Adapter属性 gridView.setAdapter(simpleAdapter); //设置选择事件,这里没有什么用可以不要 gridView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { imageView.setImageResource(imagesId[position]); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); //设置点击事件,点击事件可以是任何东西 gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { imageView.setImageResource(imagesId[position]); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-06 07:36:08