android 底部弹出菜单
2017-03-26 19:36
344 查看
好久没写笔记了,好不容易今天有空,就分享一下android底部弹出菜单的效果。
主要用popupwindow来实现。
先看一下效果图:
点击按钮就可以创建一个底部菜单了。
现在来看代码:
首先在res目录下新建anim目录,在anim目录下建两个动画效果文件,用来控制菜单的弹出和隐藏:
popshow_anim.xml:
上面的是菜单弹出时的效果, translate 位置转移动画效果 duration 属性为动画持续时间 ,fromXDelta 属性为动画起始时 X坐标上的位置 toYDelta 属性为动画结束时 Y坐标上的位置 ,属性里面还可以加上%和p,例如:
android:toXDelta=”100%”,表示自身的100%,也就是从View自己的位置开始。
android:toXDelta=”80%p”,表示父层View的80%,是以它父层View为参照的。
pophidden_anim.xml:
然后在style.xml里新建菜单的style:
然后是layout目录下:新建名为:pop.xml 的layout文件,布局弹出的窗口:
这个就不多说了。简单的一个布局。
在activity_main.xml文件里新建一个Button:用来点击:
同样button被指定了一个一个响应的函数,即openPopWindow。
做好前面的之后就可以写MainActivity了:
具体的解释代码里都有,代码不多但是实现了一个很实用的效果。想要什么样式只要在pop.xml文件里定制就行了。
主要用popupwindow来实现。
先看一下效果图:
点击按钮就可以创建一个底部菜单了。
现在来看代码:
首先在res目录下新建anim目录,在anim目录下建两个动画效果文件,用来控制菜单的弹出和隐藏:
popshow_anim.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="500" android:fromYDelta="100%p" android:toYDelta="0" /> <alpha android:duration="500" android:fromAlpha="0.0" android:toAlpha="1.0" /> </set>
上面的是菜单弹出时的效果, translate 位置转移动画效果 duration 属性为动画持续时间 ,fromXDelta 属性为动画起始时 X坐标上的位置 toYDelta 属性为动画结束时 Y坐标上的位置 ,属性里面还可以加上%和p,例如:
android:toXDelta=”100%”,表示自身的100%,也就是从View自己的位置开始。
android:toXDelta=”80%p”,表示父层View的80%,是以它父层View为参照的。
pophidden_anim.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="500" android:fromYDelta="0" android:toYDelta="50%p" /> <alpha android:duration="500" android:fromAlpha="1.0" android:toAlpha="0.0" /> </set>
然后在style.xml里新建菜单的style:
<style name="mypopwindow_anim_style"> <item name="android:windowEnterAnimation">@anim/popshow_anim</item> <!-- 指定显示的动画xml --> <item name="android:windowExitAnimation">@anim/pophidden_anim</item> <!-- 指定消失的动画xml --> </style>
然后是layout目录下:新建名为:pop.xml 的layout文件,布局弹出的窗口:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" > <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFFFF" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:divider="#bbbb" android:showDividers="middle" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button_ok" android:text="删除" android:background="#FFFFFF"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button_delete" android:text="删除" android:background="#FFFFFF" android:layout_marginBottom="10dp" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button_return" android:text="取消" android:background="#FFFFFF" /> </LinearLayout> </LinearLayout>
这个就不多说了。简单的一个布局。
在activity_main.xml文件里新建一个Button:用来点击:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="openPopWindow" android:text="点击可以弹出菜单" /> </LinearLayout>
同样button被指定了一个一个响应的函数,即openPopWindow。
做好前面的之后就可以写MainActivity了:
public class MainActivity extends AppCompatActivity { private PopupWindow popupWindow; private View contentView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showPopwindow(); } /** * 显示popupWindow */ private void showPopwindow() { //加载弹出框的布局 contentView = LayoutInflater.from(MainActivity.this).inflate( R.layout.pop, null); popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setFocusable(true);// 取得焦点 //注意 要是点击外部空白处弹框消息 那么必须给弹框设置一个背景色 不然是不起作用的 popupWindow.setBackgroundDrawable(new BitmapDrawable()); //点击外部消失 popupWindow.setOutsideTouchable(true); //设置可以点击 popupWindow.setTouchable(true); //进入退出的动画,指定刚才定义的style popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style); // 按下android回退物理键 PopipWindow消失解决 } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(event.getKeyCode()==KeyEvent.KEYCODE_BACK){ if(popupWindow!=null&&popupWindow.isShowing()){ popupWindow.dismiss(); return true; } } return false; } /** * 按钮的监听 * @param v */ public void openPopWindow(View v) { //从底部显示 popupWindow.showAtLocation(contentView, Gravity.BOTTOM, 0, 0); } }
具体的解释代码里都有,代码不多但是实现了一个很实用的效果。想要什么样式只要在pop.xml文件里定制就行了。
相关文章推荐
- Android 长按Listview 每个item底部弹出菜单
- Android PopupWindow实现右侧、左侧和底部弹出菜单
- android Activity实现从底部弹出或滑出选择菜单或窗口(不需要dialog或popupwindow)
- 【Android】Activity和PopupWindow都实现从底部弹出或滑出选择菜单或窗口
- android Activity实现从底部弹出或滑出选择菜单或窗口
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- Android实现底部弹出按钮菜单升级版
- android同时弹出底部和顶部菜单
- android 底部弹出菜单(带透明背景)
- android AlertDialog用作底部弹出菜单
- Android底部菜单布局+PopupWindows实现弹出菜单功能(初级篇)
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- Android仿网易严选底部弹出菜单效果
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- android 页面底部弹出菜单(dialog)
- android 底部弹出菜单(带透明背景)
- 【Android UI设计与开发】7.底部菜单栏(四)PopupWindow 实现显示仿腾讯新闻底部弹出菜单
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- android style 从底部弹出或滑出选择菜单窗口的样式
- android Activity实现从底部弹出或滑出选择菜单或窗口