快速弹出窗口实现四:popWindow

参考:

http://blog.csdn.net/ztp800201/article/details/13094451

一.主页面的布局

<LinearLayout
    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"
    android:orientation="vertical"
    tools:context="com.example.administrator.popwindowdemo.MainActivity">

    <Button
        android:onClick="start"
        android:text="popWindow!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

popwindow的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/pop_window_bg_selector"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/pop_window_unistall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableTop="@drawable/ic_uninstall"
        android:text="卸载" />

    <TextView
        android:id="@+id/pop_window_open"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:drawableTop="@drawable/ic_open"
        android:text="打开" />

    <TextView
        android:id="@+id/pop_window_share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:drawableTop="@drawable/ic_share"
        android:text="分享" />

    <TextView
        android:id="@+id/pop_window_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:drawableTop="@drawable/ic_info"
        android:text="信息" />

</LinearLayout>


3.代码实现

public class MainActivity extends AppCompatActivity {

    private PopupWindow mPopupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public  void  start(View view){
        //加载popWindow的布局
        View root =View.inflate(this,R.layout.pop_window,null);
        //创建popwindow的对象,绑定布局,根据父容器,指定popwindow的大小
        mPopupWindow = new PopupWindow(root, RelativeLayout.LayoutParams.WRAP_CONTENT,40);
        //拿到popwindow布局文件中的控件
        TextView uninstall = (TextView) root.findViewById(R.id.pop_window_unistall);
        //设置该控件的点击事件
       uninstall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "卸载了", Toast.LENGTH_SHORT).show();
                mPopupWindow.dismiss();
            }
        });
        //设置popWindow弹出的动画效果
        mPopupWindow.setAnimationStyle(R.style.AnimationFade);
        //设置点击外部区徐,popWindow消失
        mPopupWindow.setFocusable(true);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable());
        //设置popwindow在父容器中显示的位置
        mPopupWindow.showAtLocation(view, Gravity.CENTER,0,0);


    }
 
}
值得主页的是点击外部弹出窗口

mPopupWindow.setFocusable(true);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setBackgroundDrawable(new ColorDrawable());

设置动画

在anim设置两个进入和出去的动画

进入的动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="-100%"
        android:toXDelta="0"
        android:duration="500"
       />
</set>
出去动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="-100%"

        ></translate>

</set>

在style文件中管理动画

<style name="AnimationFade">
    <!-- PopupWindow左右弹出的效果 -->
    <item name="android:windowEnterAnimation">@anim/int_animation</item>
    <item name="android:windowExitAnimation">@anim/out_animation</item>
</style>


源码下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值