用Girdview完成九宫格

用GridView完成九宫格

先上一下效果图,原谅我懒,图片懒得找了,就都弄一样的吧。。

然后这个九宫格主要是用到了 GridView 以及Adapter 将他俩绑定一下,就可以了
用到了3个 1个主的MainActivity,1个mainactivity的xml,以及一个item的xml

那么接下来就是代码了,首先是java部分:

public class MainActivity extends AppCompatActivity {



//   private GridView gridView;
//private ImageView imageView;
//   @Override
//   protected void onCreate(Bundle savedInstanceState) {
//       super.onCreate(savedInstanceState);
//       setContentView(R.layout.activity_main);
//       gridView = (GridView) findViewById(R.id.gridview1);
//        imageView = (ImageView) findViewById(R.id.imageview);
//        SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, getData(), R.layout.activity_item, new //String[]{"img"}, new int[]{R.id.imageview});
//        gridView.setAdapter(adapter);
    
//       }
    

//    private  ArrayList<Map<String,Object>> listData = null;
//    private int[] imgId= {R.drawable.photo1,R.drawable.photo1,R.drawable.photo1,R.drawable.photo1,R.drawable.photo1,R.drawable.photo1,R.dr//awable.photo1,R.drawable.photo1,R.drawable.photo1};
//    public ArrayList<Map<String,Object>> getData(){
//        listData = new ArrayList<Map<String,Object>>();
//        for (int i= 0;i<imgId.length;i++){
//            HashMap<String,Object> map = new HashMap<String,Object>();
//            map.put("img",imgId[i]);
//            listData.add(map);
//        }
//        return (ArrayList<Map<String,Object>>) listData;
//    }

//}
然后是item的xml部分:
//<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
//    xmlns:tools="http://schemas.android.com/tools"
//    android:id="@+id/activity_item"
//    android:layout_width="match_parent"
//    android:layout_height="match_parent"
//    android:paddingBottom="@dimen/activity_vertical_margin"
//    android:paddingLeft="@dimen/activity_horizontal_margin"
//    android:paddingRight="@dimen/activity_horizontal_margin"
//    android:paddingTop="@dimen/activity_vertical_margin"
//    tools:context="cn.edu.niit.homework_5.Item">
//<ImageView
//    android:id="@+id/imageview"
//    android:layout_width="80dp"
//    android:layout_height="80dp"
//    android:src="@mipmap/ic_launcher"/>
//</LinearLayout>
 最后的最后是mianactivity.xml部分
//<LinearLayout 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"
//    android:orientation="vertical"
//    tools:context="cn.edu.niit.homework_5.MainActivity">
//<LinearLayout
//    android:layout_width="match_parent"
//   android:layout_height="match_parent"
//    android:layout_weight="5">
//    <GridView
//        android:id="@+id/gridview1"
//        android:layout_width="wrap_content"
//        android:layout_height="wrap_content"
//        android:gravity="center"
//       android:horizontalSpacing="10dp"
//        android:numColumns="3"
//        android:stretchMode="columnWidth"
//        android:verticalSpacing="5dp"
//        >
//    </GridView>
//</LinearLayout>



//</LinearLayout>

嗯,以上结束。。。

posted @ 2017-05-07 11:28  王子轩1  阅读(172)  评论(1编辑  收藏  举报