九宫格

九宫格

实现步骤:
1.用GridView展现三列布局
2.新建XML用来布局图片和文字
3.最后实现传递显示
首先,activity_main.xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.jiugongge.MainActivity">
    <GridView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"//两列之间的边距
        android:verticalSpacing="10dp"//两行之间的边距
        android:numColumns="3"/>
</RelativeLayout>

接着是新建的layout.xml的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">
    <LinearLayout
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/e"/>
        <TextView
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#000000"
            android:textSize="15dp"
            android:gravity="center"/>
    </LinearLayout>
</RelativeLayout>

最后是MainActivity.java的功能代码:

package com.example.administrator.jiugongge;
public class MainActivity extends AppCompatActivity {
    private int[] photo = {
            R.mipmap.e,
            R.mipmap.i,
            R.mipmap.o,
            R.mipmap.r,
            R.mipmap.q,
            R.mipmap.u,
            R.mipmap.w,
            R.mipmap.y,
            R.mipmap.t,
};//加入的图片集合
    private String[] name = {
            "小灰",
            "小白",
            "小黄",
            "小橙",
            "小蓝",
            "小红",
            "小绿",
            "小紫",
            "小粉"
    };//加入的文字集合
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GridView grid = (GridView) findViewById(R.id.grid);
        int length = photo.length;
		
        //生成动态数组,并且转入数据
        ArrayList<HashMap<String, Object>> lstImage = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < length; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("Image", photo[i]);
            map.put("Text", name[i]);//按序号做ItemText
            lstImage.add(map);
        }
        SimpleAdapter wangqi = new SimpleAdapter(this,
                lstImage,
                R.layout.layout,
                new String[]{"Image", "Text"},
                new int[]{R.id.img, R.id.txt});
        //添加并且显示
        grid.setAdapter(wangqi);
        }
}

效果:

posted on 2017-05-08 16:20  王启明  阅读(137)  评论(0)    收藏  举报