Android PopupMenu彈出菜單的實(shí)現(xiàn)
Android PopupMenu彈出菜單的實(shí)現(xiàn)
(1)布局文件:用于彈出菜單的處罰button:
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="96dp" android:layout_marginTop="111dp" android:onClick="showPopup" android:text="彈出菜單" /> </RelativeLayout>
(2)res-menu目錄下的main.xml文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/open" android:orderInCategory="100" android:showAsAction="never" android:title="@string/open"/> <item android:id="@+id/close" android:orderInCategory="100" android:showAsAction="never" android:title="@string/close"/> </menu>
(3)類的文件:
package com.example.menu_popupmenu; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.PopupMenu; public class MainActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void showPopup(View view) { PopupMenu popupMenu = new PopupMenu(MainActivity.this, view); popupMenu.getMenuInflater().inflate(R.menu.main, popupMenu.getMenu()); popupMenu.show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android中PopupMenu組件的使用實(shí)例
- Android編程實(shí)現(xiàn)自定義PopupMenu樣式示例【顯示圖標(biāo)與設(shè)置RadioButton圖標(biāo)】
- Android之用PopupWindow實(shí)現(xiàn)彈出菜單的方法詳解
- android使用PopupWindow實(shí)現(xiàn)頁(yè)面點(diǎn)擊頂部彈出下拉菜單
- android自定義popupwindow仿微信右上角彈出菜單效果
- Android PopupWindow實(shí)現(xiàn)右側(cè)、左側(cè)和底部彈出菜單
- Android使用Activity實(shí)現(xiàn)從底部彈出菜單或窗口的方法
- Android 使用PopupWindow實(shí)現(xiàn)彈出更多的菜單實(shí)例詳解
- Android開(kāi)發(fā)使用PopupMenu創(chuàng)建彈出式菜單完整實(shí)例
相關(guān)文章
Android開(kāi)發(fā)中給EditText控件添加TextWatcher監(jiān)聽(tīng)實(shí)現(xiàn)對(duì)輸入字?jǐn)?shù)的限制(推薦)
這篇文章主要介紹了Android開(kāi)發(fā)中給EditText控件添加TextWatcher監(jiān)聽(tīng)實(shí)現(xiàn)對(duì)輸入字?jǐn)?shù)的限制(推薦)的相關(guān)資料,需要的朋友可以參考下2016-11-11android自動(dòng)生成dimens適配文件的圖文教程詳解(無(wú)需Java工具類)
這篇文章主要介紹了android自動(dòng)生成dimens適配文件,無(wú)需Java工具類,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Android仿淘口令復(fù)制彈出框功能(簡(jiǎn)答版)
這篇文章主要介紹了Android仿淘口令復(fù)制彈出框功能(簡(jiǎn)答版)的相關(guān)資料,在文章給大家提到了淘口令原理介紹,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11Android自定義View實(shí)現(xiàn)簡(jiǎn)單文字描邊功能
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)簡(jiǎn)單文字描邊功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android 中SP與DP的區(qū)別實(shí)例詳解
這篇文章主要介紹了Android 中SP與DP的區(qū)別實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-10-10Android Studio如何查看源碼并調(diào)試的方法步驟
這篇文章主要介紹了Android Studio如何查看源碼并調(diào)試的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05深入解析Android中View創(chuàng)建的全過(guò)程
這篇文章主要給大家深入的解析了關(guān)于Android中View創(chuàng)建的全過(guò)程,文中介紹的非常詳細(xì),相信對(duì)大家會(huì)有一定的參考借鑒,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-03-03Android自定義View實(shí)現(xiàn)QQ音樂(lè)中圓形旋轉(zhuǎn)碟子
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)QQ音樂(lè)中圓形旋轉(zhuǎn)碟子,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09