会飞的鱼

奇乐云
首页 » Android » GridView之SimpleAdapter实现网格布局

GridView之SimpleAdapter实现网格布局

[1]创建一个主布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">

    <GridView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="3"/>

</LinearLayout>

[2]创建一个漂亮的引入布局在layout下

<?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:orientation="vertical"
    >

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/tv_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="菜单"/>

</LinearLayout>

[3]通过适配器进行关联起来
package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
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 gridViewMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gridViewMenu = (GridView) findViewById(R.id.grid);
        List<Map<String,String>> datalist = new ArrayList<>();

        String [] title = {"审前检查","需求评估","在册人员","请销假","集中教育","个别教育","心理评测","生活量表","矫正方案","矫正建议","出勤统计","综合查询"};

        for (int i = 0; i < 12; i++) {
            Map<String,String> map = new HashMap<>();
            map.put("menu_name",title[i]);
            datalist.add(map);
        }

        String[] from = {"menu_name"};
        int[] to ={R.id.tv_menu};
        SimpleAdapter simpleAdapter = new SimpleAdapter(this,datalist,R.layout.grid_item,from,to);
        gridViewMenu.setAdapter(simpleAdapter);

    }
}

[4]效果图
device-2019-02-27-220953.png

文章如无特别注明均为原创! 作者: 奇乐云, 转载或复制请以 超链接形式 并注明出处 奇乐云's blog - 奇乐云的个人博客
原文地址《 GridView之SimpleAdapter实现网格布局》发布于2019-2-28

分享到:
打赏

评论

游客

切换注册

登录

您也可以使用第三方帐号快捷登录

Q Q 登 录
微 博 登 录
切换登录

注册