谷歌电子市场学习笔记第一天之上
生活随笔
收集整理的這篇文章主要介紹了
谷歌电子市场学习笔记第一天之上
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、環境搭建
1.安裝最火、最快的Android模擬器:genymotion模擬器 2.安裝服務器WebServer(里面是假數據) 3.在sd卡的根目錄下放置WebInfos文件夾二、ActionBar的顯示
活動條(ActionBar)是Android3.0的重要更新之一。ActionBar位于傳統標題欄的位置,也就是顯示的屏幕的頂部。ActionBar可顯示應用的圖標和Activity標題——也就是前面應用程序的頂部顯示的內容。除此之外,ActionBar的右邊還可以顯示活動項(Action Item)。- Action Bar設計概念在Android3.0(APP 11)中被Google引入以后,在4.0版本之后更是被Google納入設計規范中,從Google的各大App中都可以看到這種設計。
- 兼容低版本
- 引入android-support-v7-appcompat
- ActionBarCompat在使用中會調用一些資源文件,尤其是基于Theme.Appcompat的主題(Theme)用來規范Action Bar的顯示。如果使用Action Bar的Activity沒有使用基于Theme.Appcompat的主題,程序就不知道該如何配制Action Bar的顯示,就會報錯導致程序退出。
- 繼承ActionBarActivity
三、ActionBar的搜素
學習參考Api Demos中的代碼: 首先看一下Api Demos中Action Bar的搜素的案例:將Api Demos的工程導入開發工具中,找到代碼:
四、搜素功能
在我們項目中實現搜素功能: 參考Android開發文檔:http://wear.techbrood.com/ res/menu/main_activity_actions.xmlMainActivity.java package com.xbmu.google; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.SearchView; import android.support.v7.widget.SearchView.OnQueryTextListener; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.Toast; public class MainActivity extends ActionBarActivity implements OnQueryTextListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {MenuInflater inflater = getMenuInflater();inflater.inflate(R.menu.main_activity_actions, menu);SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();searchView.setOnQueryTextListener(this);//設置搜素的監聽事件return true;}/**處理actionBar菜單條目的點擊事件*/@Overridepublic boolean onOptionsItemSelected(MenuItem item) {if(item.getItemId() == R.id.action_search){Toast.makeText(getApplicationContext(), "搜素", 0).show();}return super.onOptionsItemSelected(item);}//當搜素文本發生變化的時候@Overridepublic boolean onQueryTextChange(String newText) {Toast.makeText(getApplicationContext(), newText, 0).show();return false;}//當搜素提交的時候@Overridepublic boolean onQueryTextSubmit(String query) {Toast.makeText(getApplicationContext(), query, 0).show();return false;} } 運行效果:
五、ActionBar返回按鈕的處理
詳情頁面DetailActivity.java package com.xbmu.google;import android.os.Bundle; import android.support.v7.app.ActionBarActivity; public class DetailActivity extends ActionBarActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_detail);} } activity_detail.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="詳情頁面"android:layout_centerInParent="true"/> </RelativeLayout>在清單文件中聲明: <activityandroid:name="com.xbmu.google.DetailActivity"android:label="@string/app_detail"android:parentActivityName="com.xbmu.google.MainActivity" ><!-- Parent activity meta-data to support 4.0 and lower --><meta-dataandroid:name="android.support.PARENT_ACTIVITY"android:value="com.xbmu.google.MainActivity" /></activity> MainActivity.java: package com.xbmu.google; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.SearchView; import android.support.v7.widget.SearchView.OnQueryTextListener; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.Toast; public class MainActivity extends ActionBarActivity implements OnQueryTextListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ActionBar actionBar = getSupportActionBar();//設置是否將應用程序圖標轉變成可點擊的圖標,并在圖標上添加一個向左的箭頭。actionBar.setDisplayHomeAsUpEnabled(true);}public void jump(View view){Intent intent = new Intent(this, DetailActivity.class);startActivity(intent);}//...... } activity_main.xml: <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" ><Button android:layout_width="match_parent"android:layout_height="wrap_content"android:text="跳轉到詳情頁面"android:onClick="jump"/> </RelativeLayout> 運行效果圖:六、給ActionBar添加tab標簽
1.在Drawable目錄下寫一個標簽的狀態選擇器 res/drawable/actionbar_tab_indicator.xml<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><!-- STATES WHEN BUTTON IS NOT PRESSED --><!-- Non focused states --><item android:state_focused="false" android:state_selected="false"android:state_pressed="false"android:drawable="@drawable/tab_unselected" /><item android:state_focused="false" android:state_selected="true"android:state_pressed="false"android:drawable="@drawable/tab_selected" /><!-- Focused states (such as when focused with a d-pad or mouse hover) --><item android:state_focused="true" android:state_selected="false"android:state_pressed="false"android:drawable="@drawable/tab_unselected_focused" /><item android:state_focused="true" android:state_selected="true"android:state_pressed="false"android:drawable="@drawable/tab_selected_focused" /><!-- STATES WHEN BUTTON IS PRESSED --><!-- Non focused states --><item android:state_focused="false" android:state_selected="false"android:state_pressed="true"android:drawable="@drawable/tab_unselected_pressed" /><item android:state_focused="false" android:state_selected="true"android:state_pressed="true"android:drawable="@drawable/tab_selected_pressed" /><!-- Focused states (such as when focused with a d-pad or mouse hover) --><item android:state_focused="true" android:state_selected="false"android:state_pressed="true"android:drawable="@drawable/tab_unselected_pressed" /><item android:state_focused="true" android:state_selected="true"android:state_pressed="true"android:drawable="@drawable/tab_selected_pressed" /> </selector>2.實現自定義主題: res/values/themes.xml <?xml version="1.0" encoding="utf-8"?> <resources><!-- the theme applied to the application or activity --><style name="CustomActionBarTheme"parent="@style/Theme.AppCompat.Light"><item name="android:actionBarTabStyle">@style/MyActionBarTabs</item></style><!-- ActionBar tabs styles --><style name="MyActionBarTabs"parent="@style/Widget.AppCompat.ActionBar.TabView"><!-- tab indicator --><item name="android:background">@drawable/actionbar_tab_indicator</item></style> </resources>3.在MainActivity.java中添加標簽代碼: package com.xbmu.google; import android.annotation.SuppressLint; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar.Tab; import android.support.v7.app.ActionBar.TabListener; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.SearchView; import android.support.v7.widget.SearchView.OnQueryTextListener; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.Toast; public class MainActivity extends ActionBarActivity implements OnQueryTextListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ActionBar actionBar = getSupportActionBar();//設置是否將應用程序圖標轉變成可點擊的圖標,并在圖標上添加一個向左的箭頭。actionBar.setDisplayHomeAsUpEnabled(true);actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);Tab tab1 = actionBar.newTab().setText("標簽一").setTabListener(new MyTabListener());actionBar.addTab(tab1);Tab tab2 = actionBar.newTab().setText("標簽二").setTabListener(new MyTabListener());actionBar.addTab(tab2);Tab tab3 = actionBar.newTab().setText("標簽三").setTabListener(new MyTabListener());actionBar.addTab(tab3);Tab tab4 = actionBar.newTab().setText("標簽四").setTabListener(new MyTabListener());actionBar.addTab(tab4);Tab tab5 = actionBar.newTab().setText("標簽五").setTabListener(new MyTabListener());actionBar.addTab(tab5);}private class MyTabListener implements TabListener{@Overridepublic void onTabReselected(Tab arg0, FragmentTransaction arg1) {// TODO Auto-generated method stub}@Overridepublic void onTabSelected(Tab arg0, FragmentTransaction arg1) {// TODO Auto-generated method stub}@Overridepublic void onTabUnselected(Tab arg0, FragmentTransaction arg1) {// TODO Auto-generated method stub}}public void jump(View view){Intent intent = new Intent(this, DetailActivity.class);startActivity(intent);}@SuppressLint("NewApi")@Overridepublic boolean onCreateOptionsMenu(Menu menu) {MenuInflater inflater = getMenuInflater();inflater.inflate(R.menu.main_activity_actions, menu);//如果運行的環境(部署到什么版本的手機)大于3.0if(android.os.Build.VERSION.SDK_INT > 11){SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();searchView.setOnQueryTextListener(this);//設置搜素的監聽事件 }return true;}/**處理actionBar菜單條目的點擊事件*/@Overridepublic boolean onOptionsItemSelected(MenuItem item) {if(item.getItemId() == R.id.action_search){Toast.makeText(getApplicationContext(), "搜素", 0).show();}return super.onOptionsItemSelected(item);}//當搜素文本發生變化的時候@Overridepublic boolean onQueryTextChange(String newText) {Toast.makeText(getApplicationContext(), newText, 0).show();return false;}//當搜素提交的時候@Overridepublic boolean onQueryTextSubmit(String query) {Toast.makeText(getApplicationContext(), query, 0).show();return false;} }
運行效果:
七、抽屜&抽屜的開關
布局文件activity_main.xml <android.support.v4.widget.DrawerLayout 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" android:id="@+id/dl"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_centerHorizontal="true"android:layout_centerVertical="true" /><FrameLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_gravity="left"android:background="@drawable/bg_tab" /></android.support.v4.widget.DrawerLayout>MainActivity.java: package com.xbmu.google;import android.annotation.SuppressLint; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.app.FragmentTransaction; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar.Tab; import android.support.v7.app.ActionBar.TabListener; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.SearchView; import android.support.v7.widget.SearchView.OnQueryTextListener; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.Toast;public class MainActivity extends ActionBarActivity implementsOnQueryTextListener {private ActionBarDrawerToggle drawerToggle;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.dl);ActionBar actionBar = getSupportActionBar();//設置是否將應用程序圖標轉變成可點擊的圖標,并在圖標上添加一個向左的箭頭。actionBar.setDisplayHomeAsUpEnabled(true);actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);Tab tab1 = actionBar.newTab().setText("標簽一").setTabListener(new MyTabListener());actionBar.addTab(tab1);Tab tab2 = actionBar.newTab().setText("標簽二").setTabListener(new MyTabListener());actionBar.addTab(tab2);Tab tab3 = actionBar.newTab().setText("標簽三").setTabListener(new MyTabListener());actionBar.addTab(tab3);Tab tab4 = actionBar.newTab().setText("標簽四").setTabListener(new MyTabListener());actionBar.addTab(tab4);Tab tab5 = actionBar.newTab().setText("標簽五").setTabListener(new MyTabListener());actionBar.addTab(tab5);actionBar.setDisplayHomeAsUpEnabled(true);actionBar.setHomeButtonEnabled(true);//設置是否將應用程序圖標轉變成可點擊的按鈕drawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, R.drawable.ic_drawer_am, //R.string.open_drawer, R.string.close_drawer){@Overridepublic void onDrawerClosed(View drawerView) {super.onDrawerClosed(drawerView);Toast.makeText(getApplicationContext(), "抽屜關閉了", 0).show();}@Overridepublic void onDrawerOpened(View drawerView) {super.onDrawerOpened(drawerView);Toast.makeText(getApplicationContext(), "抽屜打開了", 0).show();}};mDrawerLayout.setDrawerListener(drawerToggle);//讓開關和actionBar建立關系drawerToggle.syncState();}private class MyTabListener implements TabListener {@Overridepublic void onTabReselected(Tab arg0, FragmentTransaction arg1) {// TODO Auto-generated method stub}@Overridepublic void onTabSelected(Tab arg0, FragmentTransaction arg1) {// TODO Auto-generated method stub}@Overridepublic void onTabUnselected(Tab arg0, FragmentTransaction arg1) {// TODO Auto-generated method stub}}public void jump(View view) {Intent intent = new Intent(this, DetailActivity.class);startActivity(intent);}@SuppressLint("NewApi")@Overridepublic boolean onCreateOptionsMenu(Menu menu) {MenuInflater inflater = getMenuInflater();inflater.inflate(R.menu.main_activity_actions, menu);// 如果運行的環境(部署到什么版本的手機)大于3.0if (android.os.Build.VERSION.SDK_INT > 11) {SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();searchView.setOnQueryTextListener(this);// 設置搜素的監聽事件}return true;}/** 處理actionBar菜單條目的點擊事件 */@Overridepublic boolean onOptionsItemSelected(MenuItem item) {if (item.getItemId() == R.id.action_search) {Toast.makeText(getApplicationContext(), "搜素", 0).show();}return drawerToggle.onOptionsItemSelected(item)|super.onOptionsItemSelected(item);}// 當搜素文本發生變化的時候@Overridepublic boolean onQueryTextChange(String newText) {Toast.makeText(getApplicationContext(), newText, 0).show();return false;}// 當搜素提交的時候@Overridepublic boolean onQueryTextSubmit(String query) {Toast.makeText(getApplicationContext(), query, 0).show();return false;} } 運行效果圖: 以上關于ActionBar的使用可以參考http://wear.techbrood.com/training/basics/actionbar/index.html全部代碼下載地址: http://download.csdn.net/detail/btt2013/9355011
總結
以上是生活随笔為你收集整理的谷歌电子市场学习笔记第一天之上的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于SSM实现在线考试及题库管理系统
- 下一篇: leg引擎适合什么系统的服务器,BLUE