android入门——UI(4)

GridView控件实现菜单

<?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"> <GridView
android:id="@+id/grid_view_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="3"></GridView>
</LinearLayout>

gridview_index.xml

<?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>

item_grid.xml

先使用SimpleAdapter填充数据

package com.ouc.wkp.ui1;

import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.SimpleAdapter; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* Created by wkp on 2016/8/23.
*/
public class JustLook5 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview_index); GridView gridViewMenu=(GridView)findViewById(R.id.grid_view_menu); List<Map<String,String>> dataList=new ArrayList<>(); {
Map<String, String> map = new HashMap<>();
map.put("menu_name", "拉拉拉");
dataList.add(map);
} {
Map<String, String> map = new HashMap<>();
map.put("menu_name", "嘿嘿嘿");
dataList.add(map);
} {
Map<String, String> map = new HashMap<>();
map.put("menu_name", "劈劈劈");
dataList.add(map);
} {
Map<String, String> map = new HashMap<>();
map.put("menu_name", "啪啪啪");
dataList.add(map);
} {
Map<String, String> map = new HashMap<>();
map.put("menu_name", "biubiubiu");
dataList.add(map);
} String[] from={"menu_name"};
int[] to={R.id.tv_menu}; SimpleAdapter simpleAdapter=new SimpleAdapter(this,dataList,R.layout.item_grid,from,to);
gridViewMenu.setAdapter(simpleAdapter);
}
}

JustLook5.java

再使用BaseAdapter填充数据

package com.ouc.wkp.ui1;

import android.app.Activity;
import android.media.Image;
import android.os.Bundle;
import android.view.LayoutInflater;;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* Created by wkp on 2016/8/23.
*/
public class JustLook5 extends Activity { List<MenuItem2> dataList; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview_index); GridView gridViewMenu = (GridView) findViewById(R.id.grid_view_menu); dataList = new ArrayList<>(); dataList.add(new MenuItem2("菜单1", R.drawable.menu1));
dataList.add(new MenuItem2("菜单2", R.drawable.menu2));
dataList.add(new MenuItem2("菜单3", R.drawable.menu3));
dataList.add(new MenuItem2("菜单4", R.drawable.menu4));
dataList.add(new MenuItem2("菜单5", R.drawable.menu5)); MenuAdapter menuAdapter = new MenuAdapter();
gridViewMenu.setAdapter(menuAdapter); // {
// Map<String, String> map = new HashMap<>();
// map.put("menu_name", "拉拉拉");
// dataList.add(map);
// }
//
// {
// Map<String, String> map = new HashMap<>();
// map.put("menu_name", "嘿嘿嘿");
// dataList.add(map);
// }
//
// String[] from={"menu_name"};
// int[] to={R.id.tv_menu};
//
// SimpleAdapter simpleAdapter=new SimpleAdapter(this,dataList,R.layout.item_grid,from,to);
// gridViewMenu.setAdapter(simpleAdapter);
} class MenuAdapter extends BaseAdapter { @Override
public int getCount() {
return dataList.size();
} @Override
public Object getItem(int i) {
return dataList.get(i);
} @Override
public long getItemId(int i) {
return i;
} @Override
public View getView(int i, View view, ViewGroup viewGroup) { View menuView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_grid, null); TextView tvMenuName = (TextView) menuView.findViewById(R.id.tv_menu);
tvMenuName.setText(dataList.get(i).menuName); ImageView imageViewMenu = (ImageView) menuView.findViewById(R.id.image_menu);
imageViewMenu.setImageResource(dataList.get(i).menuDrawableId); return menuView;
}
} class MenuItem2 {
String menuName;
int menuDrawableId; MenuItem2(String menuName, int menuDrawableId) {
this.menuName = menuName;
this.menuDrawableId = menuDrawableId;
}
}
}

JustLook.java

效果如下

android入门——UI(4)

上一篇:数据库(学习整理)----2--关于Oracle用户权限的授权和收权


下一篇:java操作数据库,以页面显示学生信息为例