Basic knowledge of Android [project training-implementation of project structure and main interface] [4], android Training
[This project training is a comprehensive exercise of basic Android knowledge. Note: some pictures and materials will be used in the project and they will be organized at will. A resource will be uploaded later, includes the basic functions of this item, as well as image materials]
[Project title]: Comprehensive Case Study of campus ordering App design [Objective]
The functions of the main interface are indeed complicated, so the surrounding content of the previous article is described. Now let's talk about the code and layout file of this interface.
1. Take a look at the project's organizational structure. Otherwise, it is hard to say their relationship:
(1) database-related data is included in the db package.
(2) All activities or fragment are put in eatall.
(3) entity classes in entity
(4) tools are included in util.
2. the main interface activity class is MainActivity. Note that it inherits ActivityGroup, because Fragment is not used yet.
/*** A TabHost is included in the main interface of the baimi shopping interface. As the main navigation bar, use * @ author Administrator */public class MainActivity extends ActivityGroup {TabHost tabHost; GridView gv; string mainMenus [] = {"hundred meters of food", "hundred meters of Restaurants", "recommended foods", "My favorites", "Health Tracking", "Food experiences ", "view cuisines", "my bills", "book ahead", "system help", "Hundred meters set", "Registration Information "}; int mainMenuIcons [] = new int [12]; List <Map <String, Object> menuData; // EatApp app of the main menu in Tab3; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. OnCreate (savedInstanceState); setContentView (R. layout. activity_main); tabHost = (TabHost) findViewById (R. id. mainTabHost); // initialize the initTabHost (); app = (EatApp) getApplication () ;}// private void initTabHost () {tabHost. setup (); // when using the TabHost control, you must add the program tabHost. setup (getLocalActivityManager (); TabSpec tab1 = tabHost. newTabSpec ("t1"); tab1.setIndicator ("discount", getResources (). getDrawable (R. drawable. logo_s); Intent in Tent1 = new Intent (MainActivity. this, DiscountFoodActivity. class); tab1.setContent (intent1); TabSpec tab2 = tabHost. newTabSpec ("t2"); tab2.setIndicator ("My bills", getResources (). getDrawable (R. drawable. logo_s); Intent intent2 = new Intent (MainActivity. this, AccountActivity. class); tab2.setContent (intent2); TabSpec tab3 = tabHost. newTabSpec ("t3"); tab3.setIndicator ("", getResources (). getDrawable (R. drawable. logo_s); t Ab3.setContent (R. id. main_tabMainFace); tabHost. addTab (tab1); tabHost. addTab (tab2); tabHost. addTab (tab3); // initialize the main navigation page gv = (GridView) findViewById (R. id. main_tabMainFace_gv); initGridView () ;}// initialize private void initGridView () {for (int I = 0; I <mainMenuIcons. length; I ++) {mainMenuIcons [I] = R. drawable. mainmenu_01 + I;} menuData = new ArrayList <Map <String, Object> (); for (int I = 0; I <mainMe Nus. length; I ++) {Map <String, Object> item = new HashMap <String, Object> (); item. put ("menuImg", mainMenuIcons [I]); item. put ("menuTitle", mainMenus [I]); menuData. add (item);} SimpleAdapter sa = new SimpleAdapter (this, menuData, R. layout. mainmenulist_item, new String [] {"menuImg", "menuTitle"}, new int [] {R. id. mainMenu_item_img, R. id. mainMenu_item_title}); gv. setAdapter (sa); gv. setOnItemClickListener (new OnItemClickLi Stener () {@ Overridepublic void onItemClick (AdapterView <?> Arg0, View arg1, int arg2, long arg3) {}}) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. main, menu); return true;} // click @ Overridepublic boolean onOptionsItemSelected (MenuItem item) {Intent intent = null; switch (item. getItemId () {case R. id. menu_shopcat: // view the shopping cart menu intent = new Intent (MainActivity. this, ShopcartActivity. class); break; case R. id. menu_info: // view the Personal Information Log. I ("Msg", app. userInfo + "logon information"); if (app. userInfo! = Null) {intent = new Intent (MainActivity. this, UserinfoActivity. class);} else {new LoginDialog (this);} break; case R. id. menu_setting: // system setting menu break; case R. id. menu_exit: // MainActivity of the system menu. this. finish (); break;} if (intent! = Null) startActivity (intent); return super. onOptionsItemSelected (item) ;}@ Overrideprotected void onResume () {super. onResume (); if (app. userInfo! = Null) {setTitle ("buy a hundred meters [" + app. userInfo. getUserName () + "]") ;}}/* When exiting, the data in the shopping cart */@ Overrideprotected void onDestroy () {super. onDestroy (); EatApp app = (EatApp) getApplication (); app. orderItems. clear (); app. userInfo = null; finish ();}}
The functions of several methods are described as follows:
(1)
TabHost tabHost;
This is used in the Tab navigation at the bottom.
(2) The following items are used in the third Tab.
GridView gv; String mainMenus [] = {"hundred meters of food", "hundred meters of Restaurants", "recommended foods", "My favorites", "Health Tracking ", "Food experiences", "view cuisines", "my bills", "book in advance", "system help", "Hundred meters setting", "Registration Information "}; int mainMenuIcons [] = new int [12]; data of the main menu in List <Map <String, Object> menuData; // Tab3
(3)
EatApp app ;
This is a custom Application class to allow different activities to share a part of the data]
(4) The Code involves many other activities and classes.
DiscountFoodActivity. class: This is the Activity interface for discount promotions.
AccountActivity. class: this is the bill information Activity interface.
ShopcartActivity. class: Click to view the shopping cart menu and go to the shopping cart Activity page.
UserinfoActivity. class: User information Activity Interface
New LoginDialog (this): a custom dialog box interface for User Login
3. The layout file on the main interface is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TabHost android:id="@+id/mainTabHost" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" ><LinearLayout android:id="@+id/main_tabMainFace" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <GridView android:id="@+id/main_tabMainFace_gv" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="4" android:padding="8dp" android:gravity="center_horizontal" ></GridView></LinearLayout> </FrameLayout> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" > </TabWidget> </LinearLayout> </TabHost></RelativeLayout>
Place the TabWidget below to implement tag navigation.
4. A GridView is used above. The layout file of its elements is:
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" android: gravity = "center_horizontal"> <ImageView android: id = "@ + id/mainMenu_item_img" android: layout_width = "80dp" android: layout_height = "80dp" android: scaleType = "centerCrop"/> <TextView android: id = "@ + id/mainMenu_item_title" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textSize = "16sp" android: layout_marginTop = "5dp" android: text = "menu name"/> </LinearLayout>
The next article describes how to implement the three sub-interfaces in the main interface.