我是一个新手,最近几天才接触android平台,想做一个小软件,然后想在软件中实现下图menu那样的一个东西 ,每次运行都提示错误,实在不知道怎么办了,请各位帮帮我吧~!
一下是主Activity中的代码
package test.ButtomMenu;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.GridView;
import android.widget.SimpleAdapter;
public class Buttom_Menu extends Activity {
GridView gv_buttom_menu;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
if (gv_buttom_menu == null) {
loadButtomMenu();
}
if (gv_buttom_menu.getVisibility()==View.GONE) {
gv_buttom_menu.setVisibility(View.VISIBLE);
} else {
gv_buttom_menu.setVisibility(View.GONE);
}
}
// TODO Auto-generated method stub
return super.onKeyDown(keyCode, event);
}
private void loadButtomMenu() {
gv_buttom_menu = (GridView)findViewById(R.id.gv_buttom_menu);
gv_buttom_menu.setBackgroundResource(R.drawable.icon);
gv_buttom_menu.setNumColumns(5);
gv_buttom_menu.setGravity(Gravity.CENTER);
gv_buttom_menu.setVerticalSpacing(10);
gv_buttom_menu.setHorizontalSpacing(10);
ArrayList data = new ArrayList();
HashMap map = new HashMap();
map.put("itemImage", R.drawable.icon);
map.put("itemText", "增加");
data.add(map);
map = new HashMap();
map.put("itemImage", R.drawable.icon);
map.put("itemText", "查找");
data.add(map);
map = new HashMap();
map.put("itemImage", R.drawable.icon);
map.put("itemText", "删除");
data.add(map);
map = new HashMap();
map.put("itemImage", R.drawable.icon);
map.put("itemText", "菜单");
data.add(map);
map = new HashMap();
map.put("itemImage", R.drawable.icon);
map.put("itemText", "退出");
data.add(map);
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item_menu, new String[] {"itemImage","itemText"}, new int[] {R.id.item_image,R.id.item_text});
gv_buttom_menu.setAdapter(adapter);
}
}
一下是主布局文件main.xml中的代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GridView
android:id="@+id/gv_buttom_menu"
android:layout_width="fill_parent"
android:layout_height="65sp"
android:layout_alignParentBottom="true"
android:visibility="gone"
></GridView>
</RelativeLayout>
以下是另外一个布局文件item_menu.xml中的代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dip"
>
<ImageView
android:id="@+id/item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
>
</ImageView>
<TextView
android:id="@+id/item_text"
android:layout_below="@+id/item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
>
</TextView>
</RelativeLayout>
劳烦哪位有心人帮我看一看,我定当感激涕零!