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最簡(jiǎn)單的狀態(tài)切換布局實(shí)現(xiàn)教程
這篇文章主要給大家介紹了關(guān)于Android中最簡(jiǎn)單的狀態(tài)切換布局的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10
Android View.onMeasure方法詳解及實(shí)例
這篇文章主要介紹了Android View.onMeasure方法詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android 相機(jī)相冊(cè)權(quán)限設(shè)置方法
今天小編就為大家分享一篇Android 相機(jī)相冊(cè)權(quán)限設(shè)置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Android 從底部彈出Dialog(橫向滿屏)的實(shí)例代碼
在android開(kāi)發(fā)中經(jīng)常會(huì)遇到底部彈出框的功能,今天小編抽時(shí)間給大家整理一個(gè)底部彈出橫向滿屏的dialog,需要的朋友參考下2016-11-11
Android編程實(shí)現(xiàn)GridView控件點(diǎn)擊圖片變暗效果的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)GridView控件點(diǎn)擊圖片變暗效果的方法,簡(jiǎn)單分析了GridView控件事件響應(yīng)及屬性操作的相關(guān)技巧,需要的朋友可以參考下2017-06-06
基于Flutter實(shí)現(xiàn)短信驗(yàn)證碼監(jiān)控與轉(zhuǎn)發(fā)
這篇文章主要為大家詳細(xì)介紹了如何基于Flutter實(shí)現(xiàn)短信驗(yàn)證碼監(jiān)控與轉(zhuǎn)發(fā)功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-03-03
android實(shí)現(xiàn)ViewPager的Indicator的實(shí)例代碼
本篇文章主要介紹了android實(shí)現(xiàn)ViewPager的Indicator的實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02

