第2天Menu菜单

常用的菜单

  • 1.系统菜单OptionsMenu
    步骤流程:
  • 2.上下文菜单ContextMenu
  • 3.弹出菜单

常用的菜单

菜单 显示菜单 事件监听
系统菜单 onCreateOptionsMenu onOptionsItemSelected
上下文菜单 AlertDialog.Builder() setSingleChoiceItems()

1.系统菜单OptionsMenu

第二单元 Menu菜单及PopupWindow弹窗

步骤流程:

1.在res下面创建一个menu文件夹,并新建一个xml文件作为OptionMenu的布局文件

第二单元 Menu菜单及PopupWindow弹窗

2.Activity重写onCreateOptionsMenu加载资源文件
3.Activity重写onOptionsItemSelected加设置事件监听

第二单元 Menu菜单及PopupWindow弹窗

注意:一个Activity只有一个系统菜单

2.上下文菜单ContextMenu

第二单元 Menu菜单及PopupWindow弹窗第二单元 Menu菜单及PopupWindow弹窗
步骤流程:

1.在res下面创建一个menu文件夹,并新建一个xml文件作为ContextMenu的布局文件,我们复用上面的menu布局
2.Activity重写onCreateConextMenu加载资源文件
3.Activity重写onConextItemSelected设置事件监听
4.为控件添加长按属性并将菜单绑定到这个控件上:registerForContextMenu(控件)

第二单元 Menu菜单及PopupWindow弹窗

注意:长按绑定的控件+可以为任意一个view设置上下文菜单

3.弹出菜单

第二单元 Menu菜单及PopupWindow弹窗
1.实现流程:

步骤1:在res下面创建一个menu文件夹,并新建一个xml文件作为PoupMenu的布局文件。
步骤2:把PopupMenu相关逻辑封装到showPopupMenu()方法中,包含PopupMenu的实例化、布局设置、显示、添加MenuItem的点击监听及响应等
步骤3:为控件设置事件监听直接调用showPopupMenu()方法

2.代码
(1)xml布局文件:activity_main.xml
第二单元 Menu菜单及PopupWindow弹窗
(2)Java代码:Main2Activity.java
第二单元 Menu菜单及PopupWindow弹窗

第3天PopupWindow弹出窗体

  • PopupWindow
  • 一.PopupWindow介绍
  • 二.如何自定义窗体
  • 三.实现微信QQ支付宝右上角加号弹出窗体
  • 四.底部弹出窗体
  • 五.弹出窗体背景半透明 六.设置动画
    1.在res/anim文件夹下定义进场动画
    2.定义进出场动画
    3.为popupwindow设置动画 七.自定义PopupWindow

PopupWindow

显示方法 显示位置
showAsDropDown(View anchor, int xoff, int yoff) 显示在anchor控件的下方
showAtLocation(View parent, int gravity, int x, int y) 显示在parent控件的某个位置

一.PopupWindow介绍

PopupWindow弹出窗体可以在任意位置弹出窗体,而对话框只能出现屏幕最中间

二.如何自定义窗体

(1)构造方法:public PopupWindow (Context context):context上下文对象
(2)必须设置的3大要素:
setContentView():设置自定义布局
setWidth():设置宽度
setHeight():设置高度
(3)显示窗体:
a。显示在某个指定控件的下方
showAsDropDown(View anchor):
showAsDropDown(View anchor, int xoff, int yoff);//xoff和yoff都是偏移量
b。指定父视图,显示在父控件的某个位置(Gravity.TOP,Gravity.RIGHT等)
showAtLocation(View parent, int gravity, int x, int y);
//gravity可以是Gravity.TOP、Gravity.BOTTOM、Gravity.LEFT、Gravity.RIGHT

三.实现微信QQ支付宝右上角加号弹出窗体

1.步骤流程:

步骤1.实例化PopupWindow对象
步骤2.设置自定义布局、宽度和高度
步骤3.指定位置显示: 、showAsDropDown() showAtLocation()

2代码:
(1)xml布局文件:activity_main.xml
第二单元 Menu菜单及PopupWindow弹窗
第二单元 Menu菜单及PopupWindow弹窗
第二单元 Menu菜单及PopupWindow弹窗
(2)自定义弹出窗体布局xml文件:acgtivity_popupwindow.xml
第二单元 Menu菜单及PopupWindow弹窗
第二单元 Menu菜单及PopupWindow弹窗
(3)Java代码:MainActivity.java
第二单元 Menu菜单及PopupWindow弹窗

相关文章: