从下方弹出的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位置的方法


?
1
showAsDropDown(anchor);
 以触发弹出窗的view为基准,出现在view的正下方,弹出的pop_view左上角正对view的左下角  偏移量默认为0,0


?
1
showAsDropDown(anchor, xoff, yoff);
 有参数的话,就是一view的左下角进行偏移,xoff正的向左,负的向右. yoff没测,也应该是正的向下,负的向上


?
1
showAtLocation(parent, gravity, x, y);
 
第一个参数指定PopupWindow的锚点view,即依附在哪个view上。第二个参数指定起始点第三个参数设置偏移量
希望可以帮助到大家。


0 0
原创粉丝点击