从下方弹出的PopupWindow示例 附带一些方法介绍
来源:互联网 发布:电气cad软件哪个好 编辑:程序博客网 时间:2024/05/22 03:42
今天突然想用一下PopupWindow,然后查了下资料,自己写了一个小Demo。就是点击按钮,从屏幕下方,弹出一个窗口。如下:
Acitvity部分代码:
package com.zhangs.popwindowdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
public class MainActivity extends Activity {
private Button btn_show;
private PopupWindow popupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
process();
setAllclick();
}
private void initView() {
btn_show = (Button) findViewById(R.id.btn_show);
}
private void process() {
}
private void setAllclick() {
btn_show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 将自己定义的布局文件泵出来
View popupWindow_view = getLayoutInflater().inflate(
R.layout.popwindow_item, null, false);
// 创建PopupWindow实例,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT分别是宽度和高度
popupWindow = new PopupWindow(popupWindow_view, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT, true);
// 设置动画效果
popupWindow.setAnimationStyle(R.style.AnimationFade);
// 这里是位置显示方式,在屏幕的底部,0,0分别表示X,Y的偏移量
popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
// 点击其他地方消失
popupWindow_view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
popupWindow = null;
}
return false;
}
});
}
});
}
}
主布局文件:
<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"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="SHOW THE POPWINDOW" />
</RelativeLayout>
PopupWindow的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<Button
android:id="@+id/btn_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ONE" />
<Button
android:id="@+id/btn_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TWO" />
<Button
android:id="@+id/btn_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="THREE" />
<Button
android:id="@+id/btn_four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="FOUR" />
<Button
android:id="@+id/btn_five"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="FIVE" />
<Button
android:id="@+id/btn_six"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SIX" />
</LinearLayout>
</RelativeLayout>
定义从下进入的动画xml(在res下新建anim文件夹,将这两个xml放在里面)
in_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 定义从下向上进入的动画 -->
<translate
android:duration="1000"
android:fromYDelta="100%p"
android:toYDelta="0%p" />
</set>
out_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 定义从上向下退出的动画 -->
<translate
android:duration="1000"
android:fromYDelta="0%p"
android:toYDelta="100%p" />
</set>
在values的style文件中加入
<style name="AnimationFade">
<!-- PopupWindow从下弹出的效果 -->
<item name="android:windowEnterAnimation">@anim/in_bottom</item>
<item name="android:windowExitAnimation">@anim/out_bottom</item>
</style>
下面介绍几个可以设置PopupWindow位置的方法
showAsDropDown(anchor);
showAsDropDown(anchor, xoff, yoff);
showAtLocation(parent, gravity, x, y);
第一个参数指定PopupWindow的锚点view,即依附在哪个view上。第二个参数指定起始点第三个参数设置偏移量希望可以帮助到大家。
- 从下方弹出的PopupWindow示例 附带一些方法介绍
- 从下方弹出的PopupWindow,仿iphone效果,增加半透明蒙层。
- 从底部弹出的PopupWindow
- 实现从底部弹出的PopupWindow
- PopupWindow在android5.0以上底部弹出在虚拟按键下方的解决
- Android PopupWindow 的方法 & 弹出窗口方法
- PopupWindow从上弹出
- PopupWindow从底部弹出
- 关于弹出窗口PopupWindow的实现方法
- Android popupwindow在指定控件正下方滑动弹出效果
- PopupWindow弹出框与背景变暗的实现(附带动画效果)
- android 随手记 popwindow从下方弹出
- 从屏幕底部弹出PopupWindow
- popupwindow从屏幕右上角弹出
- 从屏幕底部弹出PopupWindow
- VC弹出IE网页附带POST数据的方法
- 仿Iphone从屏幕底部弹出半透明的PopupWindow
- 手把手教会popupWindow从下往上弹出效果的实现
- POI导入2007EXCEL中遇到的文件描述符错误的问题
- JXL通过相对路径插入超链接的完美解决方案
- 想想你在form表单提交时,如果参数包含双引号怎么办
- linux_c 网络开发日记(2)makefile简介
- FLAG_ACTIVITY_CLEAR_TOP的使用
- 从下方弹出的PopupWindow示例 附带一些方法介绍
- yum安装php和mysql
- 自定义控件attrs属性介绍
- Hduoj1019【水题】
- 如何将一个Andorid工程导成JAR包
- 版本控制入门
- 第三种是经典著作阅读法
- JDK版本不兼容问题之:一台机器安装多个版本的JDK
- 输入一个n,输出2到n的具体素数值