android基础控件(4)GridView实现网格视图

1. GridView

1.1 activity_main
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/gridview1"
        android:numColumns="3"/>
</LinearLayout>
1.2 MainActivity
public class MainActivity extends AppCompatActivity {
   private int[] imglist= new int[]{
           R.drawable.img_01,R.drawable.img_02,R.drawable.img_03,R.drawable.img_04,R.drawable.
               img_05,
           R.drawable.img_06,R.drawable.img_07,R.drawable.img_08,R.drawable.img_09,R.drawable.
               img_10,
           R.drawable.img_11,R.drawable.img_12,
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GridView gridView=findViewById(R.id.gridview1);
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        for(int i=0;i<imglist.length;i++){
            Map<String,Object> map=new HashMap<String, Object>();
            map.put("image",imglist[i]);
            list.add(map);
        }
        SimpleAdapter simpleAdapter=new SimpleAdapter(MainActivity.this,list,R.layout.cell,new String[]{"image"},new int[]{R.id.image});
        gridView.setAdapter(simpleAdapter);
    }
}

1.3 SimpleAdapter的布局cell
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitXY"
    android:id="@+id/image"/>
</LinearLayout>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_33766045/article/details/107622301
今日推荐