今天在devdiv论坛里看到有坛友问到九宫格的实现,我把我在项目中用的经验分享一下,九宫格用gridview实现代码。

九宫格菜单通常是全屏显示的,那么如何控制某个Activity全屏显示呢,有两种方法:

方法一:

在该Activity的onCreate函数中添加控制代码:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);//设置Activity标题不显示
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); //设置全屏显示

注意:这两句代码必须写在setContentView函数的前面,否则运行会报错。

方法二:

使用XML配置文件进行配置:

(1) res/values/目录下新增title.xml文件,用来定义onTitle的样式,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="noTitle">
<item name="android:windowNoTitle">true</item>
</style>
</resources>

(2) 在AndroidManifest.xml文件中,对Activity添加代码:android:theme="@style/noTitle"

接下来开始开发九宫格的菜单

开发出来的界面截图:

Android之UI学习篇八:使用GridView实现九宫格的菜单_第1张图片

开发步骤:

(一) 放入图标资源到res/drawble-mdpi/目录下;

(二) 在layout下新增item.xml(对于单个格子的布局),代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/ItemImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<TextView
android:id="@+id/ItemTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>

(三) 编写Layout/main.xml文件,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MyGridView"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center">
</GridView>
</LinearLayout>

(四) MainActivity.java文件代码:

public class MainActivity extends Activity {
private GridView mGridView;//MyGridView
//定义图标数组
private int[] imageRes = { R.drawable.png1, R.drawable.png2,
R.drawable.png3, R.drawable.png4, R.drawable.png5, R.drawable.png6,
R.drawable.png7, R.drawable.png8, R.drawable.png9,
R.drawable.png10, R.drawable.png11, R.drawable.png12 };
//定义标题数组
private String[] itemName = { "审前调查", "需求评估", "在册人员", "请销假", "集中教育",
"个别教育", "心理测评", "生活量表", "矫正方案", "矫正建议", "出勤统计", "综合查询" };

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Activity标题不显示
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); // 设置全屏显示
setContentView(R.layout.main);

mGridView = (GridView) findViewById(R.id.MyGridView);
List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
int length = itemName.length;
for (int i = 0; i < length; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImageView", imageRes[i]);
map.put("ItemTextView", itemName[i]);
data.add(map);
}
//为itme.xml添加适配器
SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this,
data, R.layout.item, new String[] { "ItemImageView","ItemTextView" }, new int[] { R.id.ItemImageView,R.id.ItemTextView });
mGridView.setAdapter(simpleAdapter);
//为mGridView添加点击事件监听器
mGridView.setOnItemClickListener(new GridViewItemOnClick());
}
//定义点击事件监听器
public class GridViewItemOnClick implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
Toast.makeText(getApplicationContext(), position + "",
Toast.LENGTH_SHORT).show();
}
}
}

更多相关文章

  1. 使用shape来定义控件的一些显示属性
  2. android 自定义进度条颜色
  3. Android常用的简单代码
  4. 在android studio的虚拟机的sd卡上创建文件夹
  5. Android弹出对话框简单代码
  6. Android定义一个不消失的悬停通知栏
  7. Android 中保存图片的代码
  8. Android 代码生成界面Sample

随机推荐

  1. 无法在phpmyadmin中打开一些表
  2. PHP联系表格给用户发送随机确认号码
  3. 谈谈php里的DAO Model AR
  4. PHP:如何通过sprintf()添加前导零/零填充浮
  5. 如果不使用OAuth - PHP库,如何使用OAuth和
  6. 提交动态复选框表单并成对获取值
  7. It saves without an error, but it stil
  8. 单引号,双引号,javascript,HTML,转义字符
  9. PHP 网站模板核心类
  10. 如何在php数组中获取php键值