欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

利用PopupWindow实现无数据提醒界面

程序员文章站 2022-06-07 18:42:43
...

1、java代码

/**
 * Created by guc on 2018/6/4.
 * 描述:无任务管理
 */
public class NoTaskUtil {
    private static PopupWindow popupWindow;

    /**
     * 显示无任务布局
     *
     * @param activity
     * @param rootView
     */
    public static void showNoTaskView(final Activity activity, View rootView) {
        final View rootContentView = LayoutInflater.from(activity).inflate(R.layout.layout_notask, null);
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
        popupWindow = new PopupWindow(rootContentView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new ColorDrawable());
        popupWindow.setOutsideTouchable(false);
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(false);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });
        popupWindow.showAtLocation(rootView == null ? activity.getWindow().getDecorView() : rootView, Gravity.CENTER, 0, -SizeUtils.dp2px(50));
        rootContentView.findViewById(R.id.iv_notask).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    /**
     * 隐藏
     */
    public static void hideNoTaskView() {
        if (popupWindow != null) {
            popupWindow.dismiss();
            popupWindow = null;
        }
    }

2、空数据布局 layout_notask.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_root_no_task"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/ll_notask"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/iv_notask"
            android:layout_width="@dimen/dimen_100dp"
            android:layout_height="@dimen/dimen_100dp"
            android:src="@drawable/default_notask" />

        <TextView
            style="@style/StyleCommonText.Gray"
            android:layout_gravity="center_horizontal"
            android:text="暂无数据"
            android:textSize="14sp" />

    </LinearLayout>

</RelativeLayout>

3、使用方法

无数据时,调用 NoTaskUtil.showNoTaskView(this, rooView); 即可


相关标签: 无数据提示