仿ios的弹框

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_38729449/article/details/78957289

办法有点笨,如有好的建议希望提出来

public class BasePopup {

    protected Activity activity;
    protected PopupWindow mPopupWindow;
    protected View popupWindowView;
    View popupView;

    /**
     * 初始化PopupWindow
     *
     * @return
     */
    public View initPopuptWindowView(int resources) {
        popupView = activity.getLayoutInflater().inflate(
                resources, null, false);
        mPopupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable(activity
                .getResources(), (Bitmap) null));
        mPopupWindow.getContentView().setFocusableInTouchMode(true);
        mPopupWindow.getContentView().setFocusable(true);
        mPopupWindow.getContentView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_MENU
                        && event.getRepeatCount() == 0
                        && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (mPopupWindow != null && mPopupWindow.isShowing()) {
                        mPopupWindow.dismiss();
                    }
                    return true;
                }
                return false;
            }
        });

        popupView.setTag(mPopupWindow);
        mPopupWindow.setAnimationStyle(R.style.shareDialog);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = activity.getWindow()
                        .getAttributes();
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                lp.alpha = 1.0f;
                activity.getWindow().setAttributes(lp);
            }
        });
        return popupView;
    }

    public void show() {
        show(Gravity.BOTTOM);
    }

    public void dismiss(){
        if(mPopupWindow != null){
            mPopupWindow.dismiss();
        }
    };

    public boolean isShowing() {
        if (mPopupWindow.isShowing()) {
            return true;
        }
        return false;
    }
    public void show(int gravity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = 0.7f; // 0.0-1.0
        activity.getWindow().setAttributes(lp);
//        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        mPopupWindow.showAtLocation(activity.getWindow().getDecorView(),
                Gravity.BOTTOM, 0, 0);
        translateBottomIn(popupView);
    }


    public void translateBottomIn(View view) {
        view.clearAnimation();
        view.setAnimation(AnimationUtils.loadAnimation(activity, com.smart.androidutils.R.anim.abc_slide_in_bottom));
        view.setVisibility(View.VISIBLE);
    }

    public void translateBottomOut(View view) {
        view.clearAnimation();
        view.setAnimation(AnimationUtils.loadAnimation(activity, com.smart.androidutils.R.anim.bottom_out));
        view.setVisibility(View.GONE);
    }
}
这个才是主类
public class LinePopup extends BasePopup implements View.OnClickListener {

    private TextView dialogLineOne;
    private TextView dialogLineTwo;
    private TextView dialogLineThree;
    private TextView dialogLineFour;
    private TextView cancelButton;
    private View pop_line_one;
    private View pop_line_two;
    private View pop_line_three;

    private ILineDialogClickListener callback;

    public void setOnDialogClickListener(ILineDialogClickListener callback) {
        this.callback = callback;
    }

    public LinePopup(Activity activity) {
        this.activity = activity;
        if (popupWindowView == null || popupWindowView.getTag() == null) {
            popupWindowView = initPopuptWindowView(R.layout.dialog_line);
            dialogLineOne = (TextView) popupWindowView.findViewById(R.id.dialog_line_one);
            dialogLineTwo = (TextView) popupWindowView.findViewById(R.id.dialog_line_two);
            dialogLineThree = (TextView) popupWindowView.findViewById(R.id.dialog_line_three);
            dialogLineFour = (TextView) popupWindowView.findViewById(R.id.dialog_line_four);
            pop_line_one = popupWindowView.findViewById(R.id.pop_line_one);
            pop_line_two = popupWindowView.findViewById(R.id.pop_line_two);
            pop_line_three = popupWindowView.findViewById(R.id.pop_line_three);
            cancelButton = (TextView) popupWindowView.findViewById(R.id.btn_cancel);

            dialogLineOne.setOnClickListener(this);
            dialogLineTwo.setOnClickListener(this);
            dialogLineThree.setOnClickListener(this);
            dialogLineFour.setOnClickListener(this);
            cancelButton.setOnClickListener(this);
        }
    }

    public void setLineOneText(@StringRes int re) {
        dialogLineOne.setText(re);
        pop_line_one.setVisibility(View.GONE);
        pop_line_two.setVisibility(View.GONE);
        pop_line_three.setVisibility(View.GONE);
        dialogLineTwo.setVisibility(View.GONE);
        dialogLineThree.setVisibility(View.GONE);
        dialogLineFour.setVisibility(View.GONE);
    }

    public void setLineTwoText(@StringRes int re) {
        dialogLineTwo.setVisibility(View.VISIBLE);
        pop_line_one.setVisibility(View.VISIBLE);
        pop_line_two.setVisibility(View.GONE);
        pop_line_three.setVisibility(View.GONE);
        dialogLineThree.setVisibility(View.GONE);
        dialogLineFour.setVisibility(View.GONE);
        dialogLineTwo.setText(re);
    }


    public void setLineThreeText(@StringRes int re) {
        dialogLineThree.setVisibility(View.VISIBLE);
        pop_line_two.setVisibility(View.VISIBLE);
        pop_line_three.setVisibility(View.GONE);
        dialogLineFour.setVisibility(View.GONE);
        dialogLineThree.setText(re);
    }


    public void setLineFourText(@StringRes int re) {
        dialogLineFour.setVisibility(View.VISIBLE);
        pop_line_three.setVisibility(View.VISIBLE);
        dialogLineFour.setText(re);
    }

    public void setLineOneText(CharSequence re) {
        dialogLineOne.setText(re);
        pop_line_one.setVisibility(View.GONE);
        pop_line_two.setVisibility(View.GONE);
        pop_line_three.setVisibility(View.GONE);
        dialogLineTwo.setVisibility(View.GONE);
        dialogLineThree.setVisibility(View.GONE);
        dialogLineFour.setVisibility(View.GONE);
    }

    public void setLineTwoText(CharSequence re) {
        dialogLineTwo.setVisibility(View.VISIBLE);
        pop_line_one.setVisibility(View.VISIBLE);
        pop_line_two.setVisibility(View.GONE);
        pop_line_three.setVisibility(View.GONE);
        dialogLineThree.setVisibility(View.GONE);
        dialogLineFour.setVisibility(View.GONE);
        dialogLineTwo.setText(re);
    }


    public void setLineThreeText(CharSequence re) {
        dialogLineThree.setVisibility(View.VISIBLE);
        pop_line_two.setVisibility(View.VISIBLE);
        pop_line_three.setVisibility(View.GONE);
        dialogLineFour.setVisibility(View.GONE);
        dialogLineThree.setText(re);
    }


    public void setLineFourText(CharSequence re) {
        dialogLineFour.setVisibility(View.VISIBLE);
        pop_line_three.setVisibility(View.VISIBLE);
        dialogLineFour.setText(re);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_line_one:
                mPopupWindow.dismiss();
                if (callback != null) {
                    callback.onLineOneClick();
                }
                break;
            case R.id.dialog_line_two:
                mPopupWindow.dismiss();
                if (callback != null) {
                    callback.onLineTowClick();
                }
                break;
            case R.id.dialog_line_three:
                mPopupWindow.dismiss();
                if (callback != null) {
                    callback.onLineThreeClick();
                }
                break;
            case R.id.dialog_line_four:
                mPopupWindow.dismiss();
                if (callback != null) {
                    callback.onLineFourClick();
                }
                break;
            case R.id.btn_cancel:
                mPopupWindow.dismiss();
                break;
            default:
                break;
        }
    }

    public static abstract class ILineDialogClickListener {
        public abstract void onLineOneClick();

        public void onLineTowClick(){};

        public void onLineThreeClick(){};

        public void onLineFourClick(){};
    }

}
。XML文件
<?xml version="1.0" encoding="utf-8"?>
<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"
    android:padding="@dimen/live_padding_left">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/live_pop_window"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/dialog_line_one"
            style="@style/popLayout"/>

        <View
            android:id="@+id/pop_line_one"
            style="@style/dividing_long_line" />

        <TextView
            android:id="@+id/dialog_line_two"
            style="@style/popLayout" />

        <View
            android:id="@+id/pop_line_two"
            style="@style/dividing_long_line" />

        <TextView
            android:id="@+id/dialog_line_three"
            style="@style/popLayout"/>

        <View
            android:id="@+id/pop_line_three"
            style="@style/dividing_long_line" />

        <TextView
            android:id="@+id/dialog_line_four"
            style="@style/popLayout"/>


    </LinearLayout>

    <TextView
        android:id="@+id/btn_cancel"
        android:layout_width="match_parent"
        android:layout_height="@dimen/live_text_width"
        android:layout_marginTop="10dp"
        android:background="@drawable/live_grey_corner_bg"
        android:gravity="center"
        android:textSize="18sp"
        android:text="@string/btn_cancel"
        android:textColor="@color/tv_settings_logout" />
</LinearLayout>
stayle样式避免代码沉重,线自己写好了
<style name="popLayout">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">50dp</item>
    <item name="android:background">@drawable/live_grey_corner_bg</item>
    <item name="android:gravity">center</item>
    <item name="android:textColor">@color/tv_settings_logout</item>
    <item name="android:textSize">20sp</item>
</style>
这里我把弹出的样式也贴出来,怕有些同学懒得写
<style name="shareDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:background">@android:color/transparent</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:backgroundDimAmount">0.6</item>
</style>
到此基本结束,仿ios的弹框就算是完成了,只是这代码还可以封装下,懒得动了

展开阅读全文

没有更多推荐了,返回首页