Android简单PopWindow的实现方法。
2016-11-08 23:49
309 查看
直接上代码。
首先是布局:
activity_main.xml中:
然后popwindowlayout.xml中代码:
然后,两个动画:
popshow_anim.xml
pophidden_anim.xml:
再次。两个shape文件:
btn_shape.xml
layout_shape.xml:
最后,MainActivity代码:
当然,还需要在style.xml文件中定义动画样式。
完。效果如下
首先是布局:
activity_main.xml中:
<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" > <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击弹出" /> </RelativeLayout>
然后popwindowlayout.xml中代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="@drawable/layout_shape" android:orientation="vertical" > <Button android:id="@+id/first" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:background="@drawable/btn_shape" android:text="一" /> <Button android:id="@+id/second" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginLeft="10dp" android:background="@drawable/btn_shape" android:layout_marginRight="10dp" android:text="二" /> <Button android:id="@+id/third" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginLeft="10dp" android:background="@drawable/btn_shape" android:layout_marginRight="10dp" android:text="三" /> </LinearLayout>
然后,两个动画:
popshow_anim.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="1000" android:fromYDelta="100%p" android:toYDelta="0" /> <alpha android:duration="1000" android:fromAlpha="0.0" android:toAlpha="1.0" /> </set>
pophidden_anim.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="1000" android:fromYDelta="0" android:toYDelta="50%p" /> <alpha android:duration="1000" android:fromAlpha="1.0" android:toAlpha="0.0" /> </set>
再次。两个shape文件:
btn_shape.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:topLeftRadius="8dp" android:topRightRadius="8dp" android:bottomRightRadius="8dp" android:bottomLeftRadius="8dp"/> </shape>
layout_shape.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#f4f4f4" /> <corners android:topLeftRadius="8dp" android:topRightRadius="8dp" /> </shape>
最后,MainActivity代码:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button start = (Button) findViewById(R.id.start); start.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showPopwindow(); } }); } private void showPopwindow() { LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.popwindowlayout, null); PopupWindow window = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT); window.setFocusable(true); ColorDrawable dw = new ColorDrawable(0xffffffff); window.setBackgroundDrawable(dw); window.setAnimationStyle(R.style.PopupAnimation); window.showAtLocation(MainActivity.this.findViewById(R.id.start), Gravity.BOTTOM, 0, 0); Button first = (Button) view.findViewById(R.id.first); first.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { } }); window.setOnDismissListener(new OnDismissListener() { @Override public void onDismiss() { } }); } }
当然,还需要在style.xml文件中定义动画样式。
<style name="PopupAnimation"> <item name="android:windowEnterAnimation">@anim/popshow_anim</item> <item name="android:windowExitAnimation">@anim/pophidden_anim</item> </style>
完。效果如下
相关文章推荐
- Android popwindow简单实现弹框
- android http-post方法简单实现
- android http-post方法简单实现
- android下拉刷新上拉加载简单的实现方法;
- android用最简单的方法实现QQ5.0的侧边栏滑动效果
- Android编程简单实现ImageView点击时背景图修改的方法
- android系列之popwindow简单应用
- Android简单实现更换桌面背景的方法
- windows下简单的调用Setforegroundwindow并不能将窗口置最前,我找到三种方法可以实现该功能。
- 【Android开发经验】使用反射,得到的类的字段、方法、并实现了简单的调用
- 菜鸟初学android体验之——实现自定义简单标题栏的两种简单方法
- Android倒计时简单方法实现
- 最简单配置方法!无线网络联网状态下 VirtualBox实现Window主机与虚拟机互相访问
- Android 有道词典的简单实现方法介绍
- Android 有道词典的简单实现方法介绍
- android倒计时的简单实现方法
- Android判断屏幕是横屏或是竖屏的简单实现方法
- android中图片翻页效果简单的实现方法
- Android开发笔记----简单的方法实现打电话和浏览网页
- Android PopWindow的简单应用