本文主要是介绍PHP后端android项目(三)APP主界面九宫格,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
PHP后端android项目(三)APP主界面九宫格
代码:
https://code.csdn.net/u012995856/androidpro/tree/master
参考:
http://www.cnblogs.com/yourancao520/archive/2011/11/22/2259532.html
效果:
界面布局:
activity_main.xml
使用网格布局布局主界面
<?xml version="1.0" encoding="utf-8"?><GridView xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/gridview_main"android:layout_width="fill_parent"android:layout_height="fill_parent"android:numColumns="auto_fit"android:horizontalSpacing="10dp"android:verticalSpacing="10dp"android:columnWidth="90dp"android:stretchMode="columnWidth"android:gravity="center"tools:context="com.huijiasoft.androidpro.MainActivity"></GridView>
menu_item_main.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"><ImageView android:layout_width="wrap_content"android:id="@+id/ItemImage"android:layout_height="wrap_content"android:layout_centerHorizontal="true"/><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/ItemImage"android:id="@+id/ItemText"android:layout_centerHorizontal="true"/>
</RelativeLayout>
MainActivity.java
package com.huijiasoft.androidpro;import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;import java.util.ArrayList;
import java.util.HashMap;
/*author:pangPythonfunc:app主界面-九宫格*/public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//创建网格布局GridView gridView = (GridView) findViewById(R.id.gridview_main);//使用列表储存九宫格图片和标题ArrayList<HashMap<String,Object>> menuList = new ArrayList<HashMap<String, Object>>();String func_name[] = {"部门","员工","项目","新闻","公告","荣誉","成就","设置","关于"};//key-value形式装入集合并添加到列表中for (int i=1;i<10;i++){HashMap<String,Object> map = new HashMap<String,Object>();map.put("ItemImage",R.mipmap.ic_launcher);map.put("ItemText",func_name[i-1]);menuList.add(map);}//监听器SimpleAdapter saMenuList = new SimpleAdapter(this,menuList,R.layout.menu_item_main,new String[]{"ItemImage","ItemText"},new int[]{R.id.ItemImage,R.id.ItemText});gridView.setAdapter(saMenuList);gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {Intent intent = null;//点击九宫格进行跳转switch (i){case 0:intent = new Intent(MainActivity.this,DepartmentActivity.class);break;case 1:intent = new Intent(MainActivity.this,StaffActivity.class);break;case 2:intent = new Intent(MainActivity.this,ProjectActivity.class);break;case 3:intent = new Intent(MainActivity.this,NewsActivity.class);break;case 4:intent = new Intent(MainActivity.this,NoticeActivity.class);break;case 5:intent = new Intent(MainActivity.this,HonorActivity.class);break;case 6:intent = new Intent(MainActivity.this,AchievementActivity.class);break;case 7:intent = new Intent(MainActivity.this,SetupActivity.class);break;case 8:intent = new Intent(MainActivity.this,AboutActivity.class);break;}MainActivity.this.startActivity(intent);}});}}
认为此处逻辑代码中最重要的是监听器代码。
每个activity要在AndroidManifest.xml文件中注册,才能合法使用,还有APP要使用到的权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.huijiasoft.androidpro"><!-- Required --><permission
android:name="com.huijiasoft.androidpro.permission.JPUSH_MESSAGE"android:protectionLevel="signature" /><!-- Required 一些系统要求的权限,如访问网络等--><uses-permission android:name="com.huijiasoft.androidpro.permission.JPUSH_MESSAGE" /><uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" /><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.WAKE_LOCK" /><uses-permission android:name="android.permission.READ_PHONE_STATE" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.WRITE_SETTINGS" /><uses-permission android:name="android.permission.VIBRATE" /><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><!-- Optional for location --><uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <!-- 用于开启 debug 版本的应用在6.0 系统上 层叠窗口权限 --><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /><uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /><uses-permission android:name="android.permission.GET_TASKS" /><application
android:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".LoginActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".MainActivity" /><activity android:name=".DepartmentActivity" /><activity android:name=".StaffActivity" /><activity android:name=".ProjectActivity" /><activity android:name=".NewsActivity" /><activity android:name=".NoticeActivity" /><activity android:name=".HonorActivity" /><activity android:name=".AchievementActivity" /><activity android:name=".SetupActivity" /><activity
android:name=".AboutActivity"android:label="@string/title_activity_about"android:theme="@style/AppTheme.NoActionBar"></activity></application></manifest>
这篇关于PHP后端android项目(三)APP主界面九宫格的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!