PopupWindow与PopupMenu的用法
来源:互联网 发布:软件接口类型有哪些 编辑:程序博客网 时间:2024/05/08 05:10
PopupWindow与PopupMenu的用法
PopupMenu
PopupWindow
PopupWindow和PopupMenu的功能都是为了弹出一个窗体,不过PopupMenu的功能比较单一,而PopupWindow更强。
PopupMenu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.focus.androidnote.popupwindow.PopupWindowActivity">
<item
android:id="@+id/action_refresh"
android:title="@string/action_refresh"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
public class PopupWindowActivity extends BaseActivity {
private Button mPopWindowBtn;
private Button mPopMenuBtn;
private View mPopWindowView;
private PopupWindow mPopWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup_window);
mPopWindowBtn = (Button) findViewById(R.id.pop_window_btn);
mPopMenuBtn = (Button) findViewById(R.id.pop_menu_btn);
mPopMenu = new PopupMenu(mContext, mPopMenuBtn);
mPopMenu = new PopupMenu(mContext, mPopMenuBtn);
mPopMenu.getMenuInflater().inflate(R.menu.menu_popup_window, mPopMenu.getMenu());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_popup_window, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Toast.makeText(mContext, "Settings", Toast.LENGTH_SHORT).show();
} else if (id == R.id.action_refresh) {
Toast.makeText(mContext, "Refresh", Toast.LENGTH_SHORT).show();
}
return true;
}
public void popMenuBtnOnClick(View view) {
mPopMenu.show();
}
}
上面的代码最主要的只有3行
mPopMenu = new PopupMenu(mContext, mPopMenuBtn);
mPopMenu.getMenuInflater().inflate(R.menu.menu_popup_window, mPopMenu.getMenu());
很简单,只需要两行代码就搞定了。
期初我以为PopupMenu会和Activity的Menu共用Click事件的,但是实现onOptionsItemSelected方法后发现只有Activity的Menu会触发事件,mPopMenu依然要通过setOnMenuItemClickListener()才能实现点击事件的监听
PopupWindow
PopupWindow相比menu功能要强的多,可以实现布局更加复杂的效果
PopupMenu
PopupWindow
PopupWindow和PopupMenu的功能都是为了弹出一个窗体,不过PopupMenu的功能比较单一,而PopupWindow更强。
PopupMenu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.focus.androidnote.popupwindow.PopupWindowActivity">
<item
android:id="@+id/action_refresh"
android:title="@string/action_refresh"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
public class PopupWindowActivity extends BaseActivity {
private Button mPopWindowBtn;
private Button mPopMenuBtn;
private View mPopWindowView;
private PopupWindow mPopWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup_window);
mPopWindowBtn = (Button) findViewById(R.id.pop_window_btn);
mPopMenuBtn = (Button) findViewById(R.id.pop_menu_btn);
mPopMenu = new PopupMenu(mContext, mPopMenuBtn);
mPopMenu = new PopupMenu(mContext, mPopMenuBtn);
mPopMenu.getMenuInflater().inflate(R.menu.menu_popup_window, mPopMenu.getMenu());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_popup_window, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Toast.makeText(mContext, "Settings", Toast.LENGTH_SHORT).show();
} else if (id == R.id.action_refresh) {
Toast.makeText(mContext, "Refresh", Toast.LENGTH_SHORT).show();
}
return true;
}
public void popMenuBtnOnClick(View view) {
mPopMenu.show();
}
}
上面的代码最主要的只有3行
mPopMenu = new PopupMenu(mContext, mPopMenuBtn);
mPopMenu.getMenuInflater().inflate(R.menu.menu_popup_window, mPopMenu.getMenu());
很简单,只需要两行代码就搞定了。
期初我以为PopupMenu会和Activity的Menu共用Click事件的,但是实现onOptionsItemSelected方法后发现只有Activity的Menu会触发事件,mPopMenu依然要通过setOnMenuItemClickListener()才能实现点击事件的监听
PopupWindow
PopupWindow相比menu功能要强的多,可以实现布局更加复杂的效果
0 0
- PopupWindow与PopupMenu的用法
- PopupWindow与PopupMenu的用法
- PopupWindow与PopupMenu的用法
- Android PopupMenu 与 PopupWindow(的区别)
- Android PopupMenu 与 PopupWindow(的区别)
- PopUpWindow与PopUpMenu
- PopupMenu、PopupWindow的使用
- PopupMenu的简单用法
- 菜单PopupMenu和对话框AlertDialog、进度条对话框PopupWindow用法相关例子和自己写的案例
- PopupWindow和PopupMenu
- PopupWindow和PopupMenu
- PopupWindow和PopupMenu
- PopupMenu和PopupWindow
- 使用popupwindow设计出popupmenu效果的菜单
- PopupMenu用法
- android PopupWindow的用法
- popupwindow的用法
- PopupWindow的用法
- Android OkHttp完全解析 是时候来了解OkHttp了
- Xcode7实现本地化
- javaweb学习总结(二十二)——基于Servlet+JSP+JavaBean开发模式的用户登录注册
- python发送邮件脚本(支持多个附件,中文)
- OC_Block
- PopupWindow与PopupMenu的用法
- 给table control 添加新列
- Angular JS 在chrome中测试本地文件
- 一个常见的错误..
- SpringMVC+Spring+hibernate整合
- Redis Cluster教程
- 一次性搞定正则表达式
- 谷歌AdMob广告接入(插屏广告)
- ios中round/ceil/floorf函数的使用