Android进阶之路Android开发经验谈Android开发

popupWindow

2017-12-29  本文已影响12人  喂_balabala
@Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // 弹窗

        // contentView-显示的View, width, height-宽高
    /*  TextView contentView = new TextView(mContext);
        contentView.setText("弹窗");
        contentView.setTextSize(20);
        contentView.setBackgroundColor(Color.RED);*/
        
        View contentView = View.inflate(mContext, R.layout.pop_window_layout, null);

        final PopupWindow window = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        
        //设置默认只显示一个弹窗
        window.setBackgroundDrawable(new ColorDrawable());
        window.setFocusable(true);
        window.setOutsideTouchable(true);//设置外围点击
        
        //设置动画样式
        window.setAnimationStyle(R.style.pop_anim);
        
        //显示
        //window.showAsDropDown(view);//在哪个控件下面显示
        //window.showAsDropDown(view, 80, -60);
        
        AppBean bean = mListDatas.get(position);
        final String packageName = bean.packageName;
        
        //parent是popupWindow要在哪个夫容器里面展示
        window.showAtLocation(parent, Gravity.CENTER, 0, 0);//显示在指定位置
        
        TextView tvUninstall = (TextView) contentView.findViewById(R.id.tv_uninstall);
        TextView tvOpen = (TextView) contentView.findViewById(R.id.tv_open);
        TextView tvShare = (TextView) contentView.findViewById(R.id.tv_share);
        TextView tvInfo = (TextView) contentView.findViewById(R.id.tv_info);
        
        
        
        //判断显示或者隐藏
        tvUninstall.setVisibility(bean.isSystem ? View.GONE : View.VISIBLE);
        
        PackageManager pkgMgr = getPackageManager();
        final Intent intent = pkgMgr.getLaunchIntentForPackage(packageName);
        tvOpen.setVisibility(intent == null ? View.GONE : View.VISIBLE);
        
        
        tvUninstall.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                //实现卸载功能
                  /* <intent-filter>
                    <action android:name="android.intent.action.VIEW" />
                    <action android:name="android.intent.action.DELETE" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <data android:scheme="package" />
                </intent-filter>*/
                
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                intent.setAction("android.intent.action.DELETE");
                intent.addCategory("android.intent.category.DEFAULT");
                intent.setData(Uri.parse("package:" + packageName));
                startActivity(intent);
                //startActivityForResult(intent, requestCode)不能实现监听卸载完成
                
                window.dismiss();
            }
        });

window.dismiss();

public class AppBean {
    public Drawable icon;//图标 
    public String name;
    public boolean isInstallSD;//是否安装在SD卡
    public String space;//应用大小
    public boolean isSystem;//是否为系统应用
    public String packageName;
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/pop_bg"
    android:orientation="horizontal"
    android:padding="12dp" >

    <TextView
        android:id="@+id/tv_uninstall"
        style="@style/TextNormal"
        android:drawablePadding="4dp"
        android:drawableTop="@drawable/ic_uninstall"
        android:text="卸载" />

    <TextView
        android:id="@+id/tv_open"
        style="@style/TextNormal"
        android:layout_marginLeft="4dp"
        android:drawablePadding="4dp"
        android:drawableTop="@drawable/ic_open"
        android:text="打开" />

    <TextView
        android:id="@+id/tv_share"
        style="@style/TextNormal"
        android:layout_marginLeft="4dp"
        android:drawablePadding="4dp"
        android:drawableTop="@drawable/ic_share"
        android:text="分享" />

    <TextView
        android:id="@+id/tv_info"
        style="@style/TextNormal"
        android:layout_marginLeft="4dp"
        android:drawablePadding="4dp"
        android:drawableTop="@drawable/ic_info"
        android:text="信息" />

</LinearLayout>

<style name="pop_anim" parent="@android:style/Animation.Dialog">
        <item name="android:windowEnterAnimation">@anim/pop_enter</item>
        <item name="android:windowExitAnimation">@anim/pop_exit</item>
    </style>

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@interpolator/overshoot" >

    <translate
        android:duration="250"
        android:fromXDelta="100%"
        android:toXDelta="0" />

</set>

<!-- Animation for when a dock window at the bottom of the screen is entering. -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@interpolator/anticipate" >

        <translate
        android:duration="250"
        android:fromXDelta="0"
        android:toXDelta="100%" />

</set>

TWO

LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.pop_login, null);
//        View v = View.inflate(getContext(),R.layout.pop_login, null);


        popupWindow = new PopupWindow(v, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setFocusable(true);
        popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        popupWindow.setOutsideTouchable(true);
        //设置渐入、渐出动画效果
        popupWindow.setAnimationStyle(android.R.style.Animation_Toast);
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    popupWindow.dismiss();
                    return true;
                }
                return false;
            }
        });

   // popupWindow.update();
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
//        popupWindow.showAsDropDown(view,100,-150);

点击事件中的view就是父容器

public void onViewClicked(View view) {
    switch (view.getId()) {
case R.id.rl_touxiang_personalCenter:
    showPopWindow(view);
    break;
    }
}
上一篇下一篇

猜你喜欢

热点阅读