安卓(Android)九宫格布局介绍

1、界面如下:

安卓(Android)九宫格布局介绍_第1张图片

2、代码介绍:

  1、布局文件gridview_item.xml



    
    

2、activity代码


public class MainActivity extends AppCompatActivity {
    private List> dataList;
    private SimpleAdapter adapter;
    private GridView gridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gridView = (GridView) findViewById(R.id.gridview);
        //初始化数据
        initData();
        String[] from = {"imgId", "text"};
        int[] to = {R.id.img, R.id.text};
        //调用自带的九宫格初始化适配器
        adapter = new SimpleAdapter(this, dataList, R.layout.gridview_item, from, to);

        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView arg0, View arg1, int arg2,
                                    long arg3) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("提示").setMessage(dataList.get(arg2).get("text").toString()).create().show();
            }
        });
    }

    void initData() {
        //图标
        int icno[] = {
                R.drawable.xmgn407, R.drawable.xmgn407, R.drawable.xmgn407,
                R.drawable.xmgn407, R.drawable.xmgn407, R.drawable.xmgn407,
                R.drawable.xmgn407, R.drawable.xmgn407, R.drawable.xmgn407
        };
        //图标下的文字
        String name[] = {"图标1", "图标1", "图标1", "图标1", "图标1", "图标1", "图标1", "图标1", "图标1"};
        dataList = new ArrayList>();
        for (int i = 0; i < icno.length; i++) {
            Map map = new HashMap();
            map.put("imgId", icno[i]);
            map.put("text", name[i]);
            dataList.add(map);
        }
    }
}

源码链接:https://download.csdn.net/download/w1010166587/10854284

你可能感兴趣的:(Android,随笔)