Android PopupMenu彈出菜單的實現(xiàn)
Android PopupMenu彈出菜單的實現(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; } }
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Android中PopupMenu組件的使用實例
- Android編程實現(xiàn)自定義PopupMenu樣式示例【顯示圖標與設(shè)置RadioButton圖標】
- Android之用PopupWindow實現(xiàn)彈出菜單的方法詳解
- android使用PopupWindow實現(xiàn)頁面點擊頂部彈出下拉菜單
- android自定義popupwindow仿微信右上角彈出菜單效果
- Android PopupWindow實現(xiàn)右側(cè)、左側(cè)和底部彈出菜單
- Android使用Activity實現(xiàn)從底部彈出菜單或窗口的方法
- Android 使用PopupWindow實現(xiàn)彈出更多的菜單實例詳解
- Android開發(fā)使用PopupMenu創(chuàng)建彈出式菜單完整實例
相關(guān)文章
Android開發(fā)中給EditText控件添加TextWatcher監(jiān)聽實現(xiàn)對輸入字數(shù)的限制(推薦)
這篇文章主要介紹了Android開發(fā)中給EditText控件添加TextWatcher監(jiān)聽實現(xiàn)對輸入字數(shù)的限制(推薦)的相關(guān)資料,需要的朋友可以參考下2016-11-11android自動生成dimens適配文件的圖文教程詳解(無需Java工具類)
這篇文章主要介紹了android自動生成dimens適配文件,無需Java工具類,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03Android自定義View實現(xiàn)簡單文字描邊功能
這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)簡單文字描邊功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12Android Studio如何查看源碼并調(diào)試的方法步驟
這篇文章主要介紹了Android Studio如何查看源碼并調(diào)試的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05深入解析Android中View創(chuàng)建的全過程
這篇文章主要給大家深入的解析了關(guān)于Android中View創(chuàng)建的全過程,文中介紹的非常詳細,相信對大家會有一定的參考借鑒,需要的朋友們下面來一起學習學習吧。2017-03-03Android自定義View實現(xiàn)QQ音樂中圓形旋轉(zhuǎn)碟子
這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)QQ音樂中圓形旋轉(zhuǎn)碟子,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09