android---仿淘宝下单PopWindow

平常的popwindow没有实现跟dialog的效果,就是窗口周围还是白色的,这样不能突显出窗口,所以我们决定做一个跟dialog一样的popwindow.实现的原理其实就是在activity的布局上方放一个LinearLayout背景设置成半透明,在平常状态下不显示整个空白控件,单弹出窗口的时候就显示这个控件,窗口关闭的时候就隐藏这个控件。

Activity的布局xml   很简单就是一个用于显示的按钮和一个LinearLayout

 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button 
        android:id="@+id/popWindowBtn"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="@string/action_settings"/>
    
    <LinearLayout
        android:id="@+id/layout_all"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#90000000"
        android:orientation="vertical"
        android:visibility="gone">
        
    </LinearLayout>

</RelativeLayout>


自定义PopWindow.java

package com.example.popwindowdemo;

/**
 * @author xu
 * 2015/6/5
 * 自定义popwindow
 */
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

public class MyPopWindow implements OnDismissListener{
private Context mContext;
private PopupWindow mPopupWindow;

private OnPopListener onPopListener;
public MyPopWindow(Context context) {
// TODO Auto-generated constructor stub
this.mContext = context;
View view = LayoutInflater.from(context).inflate(R.layout.layout_popwindow, null);
mPopupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
mPopupWindow.setAnimationStyle(R.style.WindowStyle);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mPopupWindow.setOnDismissListener(this);
}
//向外部暴露接口
public void setOnPopListener(OnPopListener onPopListener){
this.onPopListener = onPopListener;
}
//关掉这个窗口
public void ondismiss(){
if(mPopupWindow != null)
mPopupWindow.dismiss();
}
/*设置窗口的位置*/
public void setWindPosition(View parent){
if(mPopupWindow != null)
{
mPopupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
mPopupWindow.setFocusable(true);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.update();
}
}

@Override
public void onDismiss() {
// TODO Auto-generated method stub
onPopListener.onLisener();
}

}

R.style.WindowStyle.主要是用于弹出和收起这个窗口的动画风格

   <style name="WindowStyle" parent="android:Animation">
         <item name="android:windowEnterAnimation">@anim/pop_up_in</item>
         <item name="android:windowExitAnimation">@anim/pop_down_out</item>
    </style>

anim/pop_up_in.xml

 
<?xml version="1.0" encoding="utf-8"?>
    <translate 
        android:duration="200"
        android:fromYDelta="100%"
        android:toYDelta="0.0"/>

</set>

 anim/pop_down_out.xml

<?xml version="1.0" encoding="utf-8"?>
    
    <translate 
        android:fromYDelta="0.0"
        android:toYDelta="100%"
        android:duration="200"/>

</set>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值