知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X(jué)表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
處女男學(xué)Android(十二)
發(fā)表時(shí)間:2020-10-19
發(fā)布人:葵宇科技
瀏覽次數(shù):47
一、媒介
轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/wlwlwlwl015/article/details/42170771
本篇blog將記錄一下Android中菜單的應(yīng)用辦法,固然在新版本中推薦應(yīng)用ActionBar去替代菜單,但我認(rèn)為菜單依然是挺不錯(cuò)的一種UI交互組件,比ActionBar好點(diǎn)一些,對(duì)于手指不靈活的人比起來(lái)ActionBar右上角的那個(gè)方塊小按鈕,或許ta會(huì)更愛(ài)好Menu呢,廢話不多說(shuō),下面就具體介紹一下Android中關(guān)于Menu的應(yīng)用辦法。
二、創(chuàng)建第一個(gè)菜單
菜蕩竽暌剮兩種創(chuàng)建方法,分別是:
- 經(jīng)由過(guò)程Java代碼創(chuàng)建菜單對(duì)象。
- 經(jīng)由過(guò)程xml資本文件定義菜單。
這里我想也不必多說(shuō),肯定應(yīng)鈣揭捉擇第二種情勢(shì),在Activity中寫代碼去創(chuàng)建菜單必定導(dǎo)致法度榜樣代碼過(guò)于癡肥,并且也不易查看和保護(hù),而第二種辦法既輕易不雅察菜單的構(gòu)造,也降低了耦合性,下面是官方給出的將“Using a menu resource”作為最佳實(shí)踐的來(lái)由,簡(jiǎn)單看一下:
[img]http://img.blog.csdn.net/20141226143227281?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2x3bHdsd2wwMTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
所以我這里就不浪費(fèi)篇幅去記錄“經(jīng)由過(guò)程代碼去創(chuàng)建菜單”了,重要記錄一下若何經(jīng)由過(guò)程xml文件定義菜單資本以及如安在Activity中引用處設(shè)置以及應(yīng)用菜單。
菜單的資本文件平日應(yīng)當(dāng)放在/res/menu目次下,菜單資本的根元素平日是<menu.../>元素,并且它不須要指定任何屬性,僅是菜單資本文件的根標(biāo)簽罷了。我們?cè)趀clipse中新建一個(gè)Android工程,在/res/menu下去new一個(gè)Menu Resource,可以看到Menu的根本構(gòu)造:
[img]http://img.blog.csdn.net/20141226150534906?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2x3bHdsd2wwMTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
可以看到<menu.../>元素內(nèi)可包含的子元素:
- <group></group>
- <item/>
下面分別解釋一下這兩個(gè)標(biāo)簽的用法和屬性,起首是最常用的<item/>,<item/>元素用于定義一個(gè)菜單項(xiàng),<item/>元素中又可以包含新的<menu/>,而位于<item/>中的<menu/>天然就代表了指定<item/>的子菜單了。下面先看一下<item/>可指定的常用屬性:
[img]http://img.blog.csdn.net/20141226151455609?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2x3bHdsd2wwMTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
平日我們必須指定的屬性只有一個(gè),就是android:title,其余的屬性都是可選的,這里我們創(chuàng)建的第一個(gè)菜單簡(jiǎn)單一些,只給出id和title即可:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/item_1" android:title="title1"/> <item android:id="@+id/item_2" android:title="title2"/> <item android:id="@+id/item_3" android:title="title3"/> <item android:id="@+id/item_4" android:title="title4"/> <item android:id="@+id/item_5" android:title="title5"/> <item android:id="@+id/item_6" android:title="title6"/> </menu>
寫好了資本文件之后,在Activity中須要重寫onCreateOptionsMenu去加載菜單,加載方法也很簡(jiǎn)單,經(jīng)由過(guò)程MenuInflater的inflate辦法即可加載菜單,完全類似于我們LayoutInflater的用法。關(guān)于菜單Item的點(diǎn)擊事宜須要在Activity中重寫onOptionsItemSelected,根據(jù)回調(diào)參數(shù)來(lái)斷定不合的Item大年夜而進(jìn)行不合的處理,其拭魅這些在新建Activity之后都邑主動(dòng)生成的,下面貼上這兩個(gè)辦法的代碼,很簡(jiǎn)單:
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.my_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); switch (id) { case R.id.item_1: Toast.makeText(this, "item_1 has been clicked!", Toast.LENGTH_SHORT) .show(); ; break; case R.id.item_2: Toast.makeText(this, "item_2 has been clicked!", Toast.LENGTH_SHORT) .show(); break; case R.id.item_3: Toast.makeText(this, "item_3 has been clicked!", Toast.LENGTH_SHORT) .show(); break; case R.id.item_4: Toast.makeText(this, "item_4 has been clicked!", Toast.LENGTH_SHORT) .show(); break; case R.id.item_5: Toast.makeText(this, "item_5 has been clicked!", Toast.LENGTH_SHORT) .show(); break; case R.id.item_6: Toast.makeText(this, "item_6 has been clicked!", Toast.LENGTH_SHORT) .show(); break; } return true; }最后看一下運(yùn)行效不雅:
[img]http://img.blog.csdn.net/20141226155022171
三、選項(xiàng)菜單(Options menu)和高低文菜單(Context menu)
google將菜單按功能特點(diǎn)分為了三種,分別是:選項(xiàng)菜單(Options menu)、高低文菜單(Context menu)以及彈出式菜單(Popup menu),如今就分別介紹一下前兩個(gè):選項(xiàng)菜單和高低文菜單。
a.選項(xiàng)菜單(Option menu)
起首看一下官方文檔中對(duì)option menu的簡(jiǎn)介:
————————————————————————————
[img]http://img.blog.csdn.net/20141226174154250?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2x3bHdsd2wwMTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
————————————————————————————
簡(jiǎn)單翻譯一下,“選項(xiàng)菜單”是一個(gè)Activity中重要的菜單項(xiàng)的集合,在它膳綾擎應(yīng)當(dāng)放置有全局性影響的功能,例如:“搜狗票、“寫竽暌故件”和“設(shè)置”。說(shuō)到這里我們差不多應(yīng)當(dāng)就能對(duì)哄人座了選項(xiàng)菜單了,安卓體系就自帶了“選項(xiàng)菜單”,也就是下圖的┞封個(gè)菜單:
[img]http://img.blog.csdn.net/20141226175340463?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2x3bHdsd2wwMTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
點(diǎn)擊手機(jī)的Menu鍵,彈出的就是我們所說(shuō)的options menu了,再看一下菜單上供給的功能,分別是:墻紙(Wallpaper)、APP治理器(Manage apps)和體系設(shè)置(System settings),這也和文檔中提到的“放置全局性影響的功能”相吻合。然則因?yàn)榇竽暌笰ndroid3.0版本開(kāi)端,并不請(qǐng)求手機(jī)上必須供給Menu鍵,并且有可能部分別機(jī)將不再供給Menu鍵(比如我的魅族MX4),在這種情況下,google推薦我們應(yīng)用ActionBar去替代options menu這種需求。
示例代碼已經(jīng)寫過(guò)了,剛才“創(chuàng)建的第一個(gè)菜單”就是一個(gè)簡(jiǎn)單標(biāo)準(zhǔn)的options menu了。
b.高低文菜單(Context menu)
起首看一下官方文檔對(duì)Context menu的概述:
[img]http://img.blog.csdn.net/20141229113616471?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2x3bHdsd2wwMTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
重點(diǎn)是第一句話,簡(jiǎn)單翻譯一下,context menu是一個(gè)浮動(dòng)的菜單,當(dāng)用戶在某個(gè)view上履行長(zhǎng)按操作時(shí)彈出。好了概念上就這么簡(jiǎn)單,那么一般什么情況下會(huì)用到它呢?其拭魅這個(gè)具體看需求,不過(guò)官方也做了相干解釋:
[img]http://img.blog.csdn.net/20141229114133120?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2x3bHdsd2wwMTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
沒(méi)錯(cuò),經(jīng)常用在ListView、GridView的Items膳綾擎,可想而知也確切如斯,經(jīng)常會(huì)有這種需求,長(zhǎng)按ListView彈出一個(gè)dialog或者一個(gè)menu,比如百度搜刮中應(yīng)當(dāng)就是一個(gè)標(biāo)準(zhǔn)的context menu了,如下圖:
[img]http://img.blog.csdn.net/20141229114518650?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2x3bHdsd2wwMTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
可以看到,當(dāng)我長(zhǎng)按一條Item的時(shí)刻,會(huì)彈出如許的一個(gè)菜單,它就是context menu,下面就具體介紹一下context menu的創(chuàng)建方法。
與options menu類似,然則在Activity中照樣有些許的差別,依舊來(lái)看一下官方文檔:
[img]http://img.blog.csdn.net/20141229115431343?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2x3bHdsd2wwMTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
可以看到,第一步就是為我們的高低文菜單注冊(cè)一個(gè)View,也就是說(shuō)當(dāng)我們長(zhǎng)按哪個(gè)View的時(shí)刻才須要彈出菜單,當(dāng)我們欲望ListView或GridView的items供給雷同的菜單的話,那么直接傳一個(gè)ListView或GridView即可完成。下面的代碼就是模仿膳綾擎百度的那個(gè)菜單的,接下來(lái)一一貼出代碼,起首是在Activity中為高低文菜單注冊(cè)一個(gè)View:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lv = (ListView) findViewById(R.id.lv_test); registerForContextMenu(lv); //為高低文菜單注冊(cè)一個(gè)ListView }下面看一下接下來(lái)的須要做的工作,文檔中也已經(jīng)說(shuō)的很清跋扈了:
[img]http://img.blog.csdn.net/20141229121054984?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2x3bHdsd2wwMTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
可以看到,這里重寫的是onCreatContextMenu辦法,而選項(xiàng)菜單重寫的是onOptionsMenu,這也就是它們最重要的差別了??梢钥吹竭@個(gè)辦法并不須要返回值,所以直接經(jīng)由過(guò)程MenuInflater加載一下菜單的資本文件就OK了。
如不雅欲望應(yīng)用法度榜樣可以或許為菜單供給響應(yīng),同樣的須要再重寫一個(gè)響應(yīng)辦法,與options menu的類似但也有些許不合:
[img]http://img.blog.csdn.net/20141229122405671?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2x3bHdsd2wwMTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
很簡(jiǎn)單,不做零丁說(shuō)清楚明了,下面就看一下仿百度的高低文菜單的完全示例代碼。
起首是activity的構(gòu)造文件:
<LinearLayout 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:orientation="vertical" > <ScrollView android:id="@+id/sv_4lv" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@drawable/top_part" > </LinearLayout> <com.wl.view.MyListView android:id="@+id/lv_test" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:entries="@array/hero" > </com.wl.view.MyListView> </LinearLayout> </ScrollView> </LinearLayout>
menu的構(gòu)造文件也很簡(jiǎn)單:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/item1" android:title="新窗口打開(kāi)"/> <item android:id="@+id/item2" android:title="后臺(tái)打開(kāi)"/> <item android:id="@+id/item3" android:title="選擇文本"/> <item android:id="@+id/item4" android:title="復(fù)制鏈接地址"/> <item android:id="@+id/item5" android:title="下載鏈接"/> </menu>
最后是Activity代碼:
package com.wl.contextmenudemo; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.ScrollView; import android.widget.TextView; import android.widget.Toast; import com.wl.model.BaiduItem; import com.wl.view.MyListView; public class MainActivity extends Activity { private BaiduItem baiduItem; private Context context; private LayoutInflater inflater; private ScrollView sv; private MyListView mlv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; inflater = LayoutInflater.from(context); sv = (ScrollView) findViewById(R.id.sv_4lv); sv.smoothScrollTo(0, 0); MyListView lv = (MyListView) findViewById(R.id.lv_test); baiduItem = new BaiduItem(); lv.setAdapter(new Myadapter()); registerForContextMenu(lv); // 為高低文菜單注冊(cè)一個(gè)ListView } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub getMenuInflater().inflate(R.menu.my_menu_2, menu); } @Override public boolean onContextItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.item1: Toast.makeText(context, "新窗口打開(kāi)!", Toast.LENGTH_SHORT).show(); break; case R.id.item2: Toast.makeText(context, "后臺(tái)打開(kāi)!", Toast.LENGTH_SHORT).show(); break; case R.id.item3: Toast.makeText(context, "選擇文本!", Toast.LENGTH_SHORT).show(); break; case R.id.item4: Toast.makeText(context, "復(fù)制鏈接地址!", Toast.LENGTH_SHORT).show(); break; case R.id.item5: Toast.makeText(context, "下載鏈接!", Toast.LENGTH_SHORT).show(); break; } return true; } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub return super.onCreateOptionsMenu(menu); } class Myadapter extends BaseAdapter { @Override public int getCount() { // TODO Auto-generated method stub return baiduItem.items.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return baiduItem.items.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View v = inflater.inflate(R.layout.my_item_1, null); TextView title = (TextView) v.findViewById(R.id.tv_title); title.setText(baiduItem.items.get(position).getTitle()); TextView content = (TextView) v.findViewById(R.id.tv_content); content.setText(baiduItem.items.get(position).getContent()); TextView url = (TextView) v.findViewById(R.id.tv_url); url.setText(baiduItem.items.get(position).getUrlAndDate()); return v; } }; }
代碼中經(jīng)由過(guò)程自定義ListView來(lái)處理和ScrollView滑動(dòng)沖突,關(guān)于數(shù)據(jù)則經(jīng)由過(guò)程封裝好的BaiduItem這個(gè)類直接供給,數(shù)據(jù)的初始化寫在這個(gè)類的靜態(tài)代碼塊中,因?yàn)橹挥?條數(shù)據(jù),所以adapter也沒(méi)有效ViewHolder優(yōu)化,整體很簡(jiǎn)單,只是模仿那個(gè)百度的效不雅裝個(gè)B,重點(diǎn)照樣懂得一下context menu的實(shí)用處合,最后看一下運(yùn)行效不雅圖:
[img]http://img.blog.csdn.net/20141231161917497
四、彈出菜單(Popup menu)
看到pop這個(gè)詞很明顯的可以知道它肯定是類似于popupwindow,是一個(gè)彈出式的菜單,并且肯定也和popupwindow有很多相似的特點(diǎn),依舊看一下官方文檔對(duì)于popupmenu的概述:
[img]http://img.blog.csdn.net/20150101202353272?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2x3bHdsd2wwMTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
簡(jiǎn)單翻譯一下,popup menu是一個(gè)??吭诠潭╒iew上的模式菜單,如不雅該View下方有空間,那么彈出菜單將顯示在該View的下方,不然會(huì)顯示在該View的上方。不難解得popup menu是依附于一個(gè)View的,也就是說(shuō)袈溱指定View的上方or下方彈出,這種菜單一般會(huì)大年夜頂部彈出或者大年夜底部彈出,在很多APP中都很常見(jiàn)這種菜單,比如微信主界面右上角的加號(hào)擱筆,點(diǎn)擊就會(huì)彈出一個(gè)popupmenu。
接下來(lái)介紹一下popup menu的創(chuàng)建方法,同popup window很類似,僅需三步:
1.經(jīng)由過(guò)程其構(gòu)造辦法new PopupMenu(Context context,View anchor)創(chuàng)建下拉菜單,第二個(gè)參數(shù)即表示本菜單所依附的View。
2.調(diào)用MenuInflater的inflate()辦法裝載menu的資本文件。
3.調(diào)用popup menu的show()辦法來(lái)顯示菜單即可。
很簡(jiǎn)單吧,也沒(méi)啥好說(shuō)的,下面就模仿微信的那個(gè)彈出菜單做一個(gè)小例子,直接貼代碼。
Activity構(gòu)造文件:
<LinearLayout 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:orientation="vertical" > <LinearLayout android:id="@+id/ll_top_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#000" android:orientation="horizontal" android:padding="8dp" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:text="我不是微信" android:textColor="#ccc" android:textSize="16sp" /> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > <ImageButton android:id="@+id/ibtn_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/ic_search" /> <ImageButton android:id="@+id/ibtn_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:background="@drawable/ic_add" /> </LinearLayout> </LinearLayout> </LinearLayout>
Activity代碼:
package com.wl.menusdemo; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.KeyEvent; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageButton; import android.widget.PopupMenu; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private ImageButton ibtnAdd; private ImageButton ibtnSearch; private PopupMenu popMenu; private Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; ibtnAdd = (ImageButton) findViewById(R.id.ibtn_add); ibtnAdd.setOnClickListener(this); ibtnSearch = (ImageButton) findViewById(R.id.ibtn_search); } @Override public void onClick(View v) { // TODO Auto-generated method stub if (v == ibtnAdd) { popMenu = new PopupMenu(context, ibtnAdd); getMenuInflater().inflate(R.menu.pop_menu, popMenu.getMenu()); popMenu.show(); popMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.item_chart: Toast.makeText(context, "提議群聊!", Toast.LENGTH_SHORT) .show(); break; case R.id.item_add_friend: Toast.makeText(context, "添加同伙!", Toast.LENGTH_SHORT) .show(); break; case R.id.item_sao: Toast.makeText(context, "掃一掃!", Toast.LENGTH_SHORT) .show(); break; case R.id.item_idea: Toast.makeText(context, "看法反饋!", Toast.LENGTH_SHORT) .show(); break; } return false; } }); } } }
是不是超簡(jiǎn)單的說(shuō),下面看一下運(yùn)行效不雅:
[img]http://img.blog.csdn.net/20150101212405724
五、模仿手機(jī)QQ的底部彈出菜單
介紹完了所有類型的菜單之后,最后再做一個(gè)演習(xí),可能我們也發(fā)清楚明了菜單的樣式比較局限,一般很難做一些定制的樣式,所以說(shuō)還有一種很不錯(cuò)的解決方檀卷是不應(yīng)用Menu的API,經(jīng)由過(guò)程監(jiān)聽(tīng)菜單鍵的KEY CODE來(lái)彈出一個(gè)popup window,如許的話我們做一些個(gè)性化的樣式、動(dòng)畫等等就沒(méi)有那么多限制了,在這里我就經(jīng)由過(guò)程這種方法來(lái)模仿一下手機(jī)QQ的功能菜單,下面是效不雅圖:
[img]http://img.blog.csdn.net/20150102180313515?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2x3bHdsd2wwMTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
可以看到,當(dāng)我們進(jìn)入QQ之后點(diǎn)擊手機(jī)的Menu鍵時(shí),會(huì)大年夜底部彈出圖中圈出的菜單,下面就結(jié)合popup window來(lái)實(shí)現(xiàn)這個(gè)效不雅,起首將須要做的工作拆分為三個(gè)步調(diào):
step 1
在Activity中重寫onKeyDown()辦法,并經(jīng)由過(guò)程KEY CODE來(lái)監(jiān)聽(tīng)菜單鍵按下的事宜,并在該事宜中彈出一個(gè)popup window。
step 2
構(gòu)造popup window對(duì)象,并編寫構(gòu)造文件代碼,設(shè)制揭捉式以及動(dòng)畫。
step 3
點(diǎn)擊菜單鍵時(shí)show這個(gè)popup window,點(diǎn)擊菜單鍵或者popup window之外的區(qū)域時(shí)dismiss這個(gè)popup window。
好了,接下來(lái)就按步調(diào)去實(shí)現(xiàn)它,起首是第一步,重寫onKeyDown()辦法用來(lái)響應(yīng)菜單鍵的單擊,直接貼代碼:
package com.wl.menusdemo; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.KeyEvent; import android.widget.Toast; public class SecondActivity extends Activity { private Context context; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.second); context = this; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (keyCode == KeyEvent.KEYCODE_MENU) { Toast.makeText(context, "預(yù)備好,要彈出菜單了!", Toast.LENGTH_LONG).show(); } return true; } }
很簡(jiǎn)單,看一下效不雅圖:
[img]http://img.blog.csdn.net/20150102190024953
第二步和第三步一路說(shuō)了,編寫popup window的構(gòu)造文件代碼,我能想到的最簡(jiǎn)單的辦法就是經(jīng)由過(guò)程一組Button來(lái)實(shí)現(xiàn),然后須要為popupwindow添加一個(gè)彈出動(dòng)畫,直接看代碼,下面分別是popupwindow的構(gòu)造文件和Activity中的全部代碼。
popup_menu.xml:
<?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" > <Button android:id="@+id/btn_ver_update" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/my_btn_selector_2" android:gravity="center" android:padding="5dp" android:text="版本更新" android:textColor="#62A6E7" android:textSize="20sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="2dp" android:background="#ccc" > </LinearLayout> <Button android:id="@+id/btn_help" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/my_btn_selector_3" android:gravity="center" android:padding="5dp" android:text="贊助與反饋" android:textColor="#62A6E7" android:textSize="20sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="2dp" android:background="#ccc" > </LinearLayout> <Button android:id="@+id/btn_exit_qq" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/my_btn_selector_4" android:gravity="center" android:padding="5dp" android:text="退出QQ" android:textColor="#62A6E7" android:textSize="20sp" /> <Button android:id="@+id/btn_undo" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginTop="8dp" android:background="@drawable/my_btn_selector" android:gravity="center" android:padding="5dp" android:text="撤消" android:textColor="#62A6E7" android:textSize="20sp" /> </LinearLayout>
Activity:
package com.wl.menusdemo; import android.app.ActionBar.LayoutParams; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.WindowManager; import android.widget.Button; import android.widget.PopupWindow; import android.widget.PopupWindow.OnDismissListener; import android.widget.Toast; public class SecondActivity extends Activity implements OnClickListener { private Context context; private PopupWindow popupWindow; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.second); context = this; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (keyCode == KeyEvent.KEYCODE_MENU) { showMenu(); return true; } return super.onKeyDown(keyCode, event); } private void showMenu() { LayoutInflater inflater = getLayoutInflater(); View menuView = inflater.inflate(R.layout.pop_menu, null); Button button1 = (Button) menuView.findViewById(R.id.btn_ver_update); Button button2 = (Button) menuView.findViewById(R.id.btn_help); Button button3 = (Button) menuView.findViewById(R.id.btn_exit_qq); Button button4 = (Button) menuView.findViewById(R.id.btn_undo); button1.setOnClickListener(this); button2.setOnClickListener(this); button3.setOnClickListener(this); button4.setOnClickListener(this); // 設(shè)置PopupWindow寬高 DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); double screenWidth = metrics.widthPixels * 0.95; popupWindow = new PopupWindow(menuView, (int) screenWidth, LayoutParams.WRAP_CONTENT); // 想要讓PopupWindow中的控件可以或許應(yīng)用,就必須設(shè)置PopupWindow為focusable popupWindow.setFocusable(true); // 想做到在你點(diǎn)擊PopupWindow以外的區(qū)域時(shí)刻讓PopupWindow消掉就做如下兩步操作 // 1:設(shè)置setOutsideTouchable為ture,這個(gè)很輕易懂得吧,跟AlertDialog一樣的 popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(getResources().getDrawable( android.R.color.transparent)); popupWindow.setAnimationStyle(R.style.AnimationPreview); popupWindow.setOnDismissListener(onDismissListener); popupWindow.showAtLocation(getWindow().getDecorView(), android.view.Gravity.BOTTOM, 0, 0); // 設(shè)置父窗口透明度 WindowManager.LayoutParams wlp = getWindow().getAttributes(); wlp.alpha = 0.5f; getWindow().setAttributes(wlp); } private OnDismissListener onDismissListener = new OnDismissListener() { @Override public void onDismiss() { WindowManager.LayoutParams wlp = getWindow().getAttributes(); wlp.alpha = 1.0f; getWindow().setAttributes(wlp); } }; @Override public void onClick(View v) { // TODO Auto-generated method stub if (popupWindow != null && popupWindow.isShowing()) popupWindow.dismiss(); switch (v.getId()) { case R.id.btn_ver_update: Toast.makeText(context, "版本更新!", Toast.LENGTH_SHORT).show(); break; case R.id.btn_help: Toast.makeText(context, "贊助與反饋!", Toast.LENGTH_SHORT).show(); break; case R.id.btn_exit_qq: Toast.makeText(context, "退出QQ!", Toast.LENGTH_SHORT).show(); break; case R.id.btn_undo: Toast.makeText(context, "撤消!", Toast.LENGTH_SHORT).show(); break; } } }
BINGO~停止了,最后看一下效不雅圖,相似度還不錯(cuò)吧:
[img]http://img.blog.csdn.net/20150103151903718
關(guān)于樣式的話,每個(gè)按鈕都是經(jīng)由過(guò)程selector+shape嵌套實(shí)現(xiàn)的,popup window彈出的時(shí)刻還設(shè)置了一個(gè)異常簡(jiǎn)單的補(bǔ)間動(dòng)畫,配風(fēng)景直接用透明色值就OK了,下面把兩個(gè)動(dòng)畫的資本文件和一個(gè)按鈕的樣式資本文件貼出來(lái)(其余的類似)。
anim_bottom_in.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="350" android:fromYDelta="100%" android:toYDelta="0.0" /> </set>
anim_bottom_out.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="350" android:fromYDelta="0.0" android:toYDelta="100%p" /> </set>
my_btn_selector.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false"><shape android:shape="rectangle"> <gradient android:endColor="#efffffff" android:startColor="#efffffff"></gradient> <!-- 設(shè)置內(nèi)填充 --> <padding android:bottom="7dp" android:left="7dp" android:right="7dp" android:top="7dp" /> <!-- 設(shè)置圓角矩形 --> <corners android:radius="8dp" /> </shape></item> <item android:state_pressed="true"><shape android:shape="rectangle"> <gradient android:endColor="#ecdcdcdc" android:startColor="#ecdcdcdc"></gradient> <!-- 設(shè)置內(nèi)填充 --> <padding android:bottom="7dp" android:left="7dp" android:right="7dp" android:top="7dp" /> <!-- 設(shè)置圓角矩形 --> <corners android:radius="8dp" /> </shape></item> </selector>
Over,如不雅新手同伙細(xì)心看了本篇blog的話信賴必定會(huì)有不少的收成的~ 我小我有以下收成:
1.進(jìn)修了Android中的3種Menu的概念和R單的應(yīng)用辦法(Options menu、Context menu、Popup menu)。
2.進(jìn)修了Android簡(jiǎn)單動(dòng)畫的編寫和引用。
3.進(jìn)修了selector和shape的嵌套。
4.進(jìn)修并復(fù)習(xí)了以下的幾個(gè)小常識(shí)點(diǎn):改變popupwindow父窗體透明度、shape資本中關(guān)于4個(gè)corners的隨便率性控制、根據(jù)DisplayMetrix來(lái)設(shè)置popupwindow的寬度、根據(jù)KEY CODE監(jiān)聽(tīng)菜單鍵的動(dòng)作、關(guān)于ScrollView嵌套ListView滾動(dòng)沖突的問(wèn)題等等。
六、總結(jié)
也許這一篇blog的標(biāo)題猜吸惹人,內(nèi)容也沒(méi)有其余大年夜神寫的那么深刻高端,但我認(rèn)為這算是我寫過(guò)的最賣力的博客之一了,用了很長(zhǎng)時(shí)光。大年夜翻譯文檔、修改圖片到錄制gif,對(duì)每一個(gè)常識(shí)點(diǎn)和概念句斟字嚼,推敲再三,不放過(guò)每一個(gè)細(xì)節(jié),總的來(lái)說(shuō)我的收成照樣蠻大年夜的,至少在我的下一APP中我必定會(huì)用到膳綾擎寫的那個(gè)仿QQ的“菜單”。在往后的blog中我還會(huì)盡我最大年夜的盡力去做到“賣力”、“細(xì)心”和“完美”,因?yàn)槲沂翘幣?。好了,廢話不多說(shuō),持續(xù)滾去加班寫辦事端了,但同時(shí)Android的進(jìn)修我也毫不會(huì)拉下的,以csdn為證!加油,Raito!
相關(guān)案例查看更多
相關(guān)閱讀
- 昆明做網(wǎng)站
- 網(wǎng)站建設(shè)首頁(yè)
- 昆明網(wǎng)站設(shè)計(jì)
- 支付寶小程序被騙
- 微分銷
- 云南網(wǎng)站建設(shè)哪家公司好
- 汽車報(bào)廢
- 云南網(wǎng)站建設(shè)方案 doc
- 云南衛(wèi)視小程序
- 小程序商城
- 汽車報(bào)廢回收軟件
- 網(wǎng)站優(yōu)化哪家好
- 云南網(wǎng)站建設(shè)價(jià)格
- 網(wǎng)站建設(shè)公司網(wǎng)站
- 汽車回收管理
- 網(wǎng)站上首頁(yè)
- 網(wǎng)站建設(shè)需要多少錢
- 汽車回收系統(tǒng)
- 百度推廣
- 南通小程序制作公司
- 區(qū)塊鏈
- 云南小程序開(kāi)發(fā)公司
- 云南旅游網(wǎng)站建設(shè)
- 開(kāi)發(fā)框架
- 網(wǎng)站建設(shè)方法
- 云南網(wǎng)站建設(shè)專業(yè)品牌
- 昆明軟件公司
- 小程序定制
- 汽車報(bào)廢系統(tǒng)
- 小程序被騙