本文实例讲述了Android编程之九宫格实现方法。分享给大家供大家参考,具体如下:
显示九宫格需要用GridView , 要显示每个格子中的视图有两种方式,第一种方式是做成xml文件,再将xml文件做成视图。第二种方式就是在代码中构建出这样一种布局,这里采用第一种方式来实现:
GridView:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<? 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 = "fill_parent" > <!-- id gv_all 宽高都是填充父窗体 numcolums 为3 水平控件的距离 10px 垂直距离是10px gridview 离底部58px 离顶部28px 离左边5px 离右边5px --> < GridView android:id = "@+id/gv_all" android:layout_height = "fill_parent" android:layout_width = "fill_parent" android:numColumns = "3" android:horizontalSpacing = "10px" android:verticalSpacing = "10px" android:layout_marginBottom = "58px" android:layout_marginTop = "28px" android:layout_marginLeft = "5px" android:layout_marginRight = "5px" ></ GridView > </ RelativeLayout > |
视图:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "90px" android:layout_height = "90px" > < ImageView android:layout_width = "64px" android:layout_height = "64px" android:layout_gravity = "center_horizontal" android:id = "@+id/main_gv_iv" /> < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_gravity = "center_horizontal" android:textSize = "16px" android:textColor = "#FFF" android:id = "@+id/main_gv_tv" /> </ LinearLayout > |
初始化:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
public class MainActivity extends Activity { public static final String TAG = "MainActivity" ; GridView maingv; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); //全屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.mainactivity); //获取到GridView maingv = (GridView) this .findViewById(R.id.gv_all); //给gridview设置数据适配器 maingv.setAdapter( new MainGridViewAdapter( this )); //点击事件 maingv.setOnItemClickListener( new MainItemClickListener()); } private class MainItemClickListener implements OnItemClickListener{ /** * @param parent 代表当前的gridview * @param view 代表点击的item * @param position 当前点击的item在适配中的位置 * @param id 当前点击的item在哪一行 */ public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switch (position) { case 0 : Intent intent = new Intent(MainActivity. this ,LostProtectedActivity. class ); startActivity(intent); break ; } } } } |
设置数据适配器 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
// 完成gridview 数据到界面的适配 public class MainGridViewAdapter extends BaseAdapter { private static final String TAG = "MainGridViewAdapter" ; private String[] names = { "手机防盗" , "通讯卫士" , "软件管理" , "任务管理" , "上网管理" , "手机杀毒" , "系统优化" , "高级工具" , "设置中心" }; private int [] icons = {R.drawable.safe,R.drawable.callmsgsafe,R.drawable.app,R.drawable.taskmanager,R.drawable.netmanager,R.drawable.trojan,R.drawable.sysoptimize,R.drawable.atools,R.drawable.settings}; private Context context; LayoutInflater infalter; public MainGridViewAdapter(Context context) { this .context = context; //方法1 通过系统的service 获取到 试图填充器 //infalter = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //方法2 通过layoutinflater的静态方法获取到 视图填充器 infalter = LayoutInflater.from(context); } // 返回gridview里面有多少个条目 public int getCount() { return names.length; } //返回某个position对应的条目 public Object getItem( int position) { return position; } //返回某个position对应的id public long getItemId( int position) { return position; } //返回某个位置对应的视图 public View getView( int position, View convertView, ViewGroup parent) { Log.i(TAG, "GETVIEW " + position); //把一个布局文件转换成视图 View view = infalter.inflate(R.layout.mainactivity_item, null ); ImageView iv = (ImageView) view.findViewById(R.id.main_gv_iv); TextView tv = (TextView) view.findViewById(R.id.main_gv_tv); //设置每一个item的名字和图标 iv.setImageResource(icons[position]); tv.setText(names[position]); return view; } } |
希望本文所述对大家Android程序设计有所帮助。