今天在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的样式,代码如下:
(2) 在AndroidManifest.xml文件中,对Activity添加代码:android:theme="@style/noTitle"
接下来开始开发九宫格的菜单
开发出来的界面截图:
开发步骤:
(一) 放入图标资源到res/drawble-mdpi/目录下;
(二) 在layout下新增item.xml(对于单个格子的布局),代码如下:
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:id="@+id/ItemImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
android:id="@+id/ItemTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
(三) 编写Layout/main.xml文件,代码如下:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
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">
(四) 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