Android 基础 popupWindow 提示框



弹出框-----popupWindow

 

Android对话框的一种(阻塞线程所以这里不能做耗时操作),另外一种是AlertDialog(不阻塞线程)

popupWindow按照有无偏移分为有偏移无偏移按相对位置分可以分为相对某一控件相对父容器

方法如下

showAsDropDown(View ancher)相对于某个空间的位置(正左下方),无偏移

showAsDropDown(View anchor,int xoff,int yoff)相对与某个空间的位置,有偏移。

showAtLocation(View parent,int gravity,int x,int y)相对于父控件的位置(Gravity.CENTER,正中,Gravity.BOTTOM,下方)也可以设置有无偏移

 

具体的实现步骤

 

activity中创建一个popupWindow的对象window

参数一给popupwindow设置的布局(用布局加载器加载进来),参数二,三:布局的宽高 高一般设置为包裹内容

ViewGroup.LayoutParams.WRAP_CONTENT

popupWindow window=new popupWindow(View contentView,int width,int height);

 

window.setFocusable(true);

默认是false popupwindow没有获取焦点的能力 这里将其置为truepopupwindow具有获取焦点的

能力这样才能操作上面的输入框等

window.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));

设置popupwindow的背景因为是drawable类型无法直接放资源需要下面的方式来获取也可以直接创建一个

window.setAnimationStyle(R.style.popwin_anim_style);

popupwindow设置动画(此动画需要在styles资源中要先定义好两个动画(一个打开一个收起)在放在资源文件里)

<style name="popwin_anim_style">  

     <item name="android:windowEnterAnimation">@anim/pop</item>  

       <item name="android:windowExitAnimation">@anim/out</item>  

</style> 

 

window.setshowAsDropDown(button);

设置显示的位置在某个控件的下方

public class MainActivity extends Activity {

public Button button;

public PopupWindow popupWindow;

View view;

LayoutInflater inflater;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

button = (Button) findViewById(R.id.btn1);

button.setOnClickListener(l);

inflater = LayoutInflater.from(this);

}

OnClickListener l = new OnClickListener() {

@Override

public void onClick(View v) {

if (popupWindow == null) {

view = inflater.inflate(R.layout.popupwindowsnull);

popupWindow = new PopupWindow(view, 200,

ViewGroup.LayoutParams.WRAP_CONTENT);

popupWindow.setFocusable(true);// 设置点击任意位置收回

popupWindow.setBackgroundDrawable(getResources().getDrawable(

R.drawable.ic_launcher));

popupWindow.setAnimationStyle(R.style.popwin_anim_style);

}

// popupWindow.showAsDropDown(button);//在谁的下面显示

View view2 = inflater.inflate(R.layout.activity_mainnull);

if (popupWindow.isShowing()) {

popupWindow.dismiss();

}else{

popupWindow.showAtLocation(view2, Gravity.BOTTOM,-100, 30);

}

}

};

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值