替换View.

有时候我们需要对网络状态,无数据等情况,添加一个统一的样式.如果直接添加到代码或者xml中,则每个布局都需要写.而且因为每个布局都不一样,导致所有的页面的操作方式都不相同.
这里提供一个通用的替换方法,可以很方便的替换掉指定的View

/**
     * @方法名: replaceView
     * @功能描述:替换View,替换成指定的View
     * @param ac
     *            当前Activity
     * @param id
     *            需要替换的View的id
     * @param view
     *            替换View
     * @return void
     */
    public static void replaceView(FragmentActivity ac, int id, View view) {
        if (ac == null) {
            Log.e("siyehua-status", "Activity has been destory!");
            return;
        }
        View rootView = ac.findViewById(android.R.id.content);
        if (rootView != null && rootView instanceof FrameLayout) {
            FrameLayout rootGroup = (FrameLayout) rootView;
            View childView = rootGroup.findViewById(id);
            if (childView != null) {// 如果有这个View
                rootGroup.addView(view);
                FrameLayout.LayoutParams layoutParams = (LayoutParams) view
                        .getLayoutParams();
                layoutParams.width = childView.getWidth();// 设置被添加进来的View的宽度
                layoutParams.height = childView.getHeight();// 设置被添加进来的View的高度
                layoutParams.setMargins(childView.getLeft(),
                        childView.getTop(), 0, 0);// 设置被添加进来的View的位置
                view.setLayoutParams(layoutParams);
            } else {
                Log.e("siyehua-status",
                        "The view  isn't exist which you want to replaced! Please check the  view's id.");
            }
        } else {
            Log.e("siyehua-status",
                    "The rootView isn't exist! Please check the rootView's id in code.");
        }
    }

    /**
     * @方法名: remmoveReplaceView
     * @功能描述:移除替换的View
     * @param ac
     *            当前Activity
     * @param view
     *            上次被添加进来的View
     * @return void
     */
    public static void remmoveReplaceView(FragmentActivity ac, View view) {
        if (ac == null) {
            Log.e("siyehua-status", "Activity has been destory!");
            return;
        }
        View rootView = ac.findViewById(android.R.id.content);
        if (rootView != null && rootView instanceof FrameLayout) {
            FrameLayout rootGroup = (FrameLayout) rootView;
            rootGroup.removeView(view);
        } else {
            Log.e("siyehua-status",
                    "The rootView isn't exist! Please check the rootView's id in code.");
        }
    }

    private static TextView toastView;

    /**
     * @方法名: replaceToastView
     * @功能描述:在View中添加提示
     * @param ac
     *            当前Activity
     * @param id
     *            需要被替换的view的id
     * @param content
     *            添加的文字
     * @param textSize
     *            文字的大小
     * @param textColor
     *            文字的颜色
     * @return void
     */
    public static void replaceToastView(FragmentActivity ac, int id,
            String content, int textSize, int textColor) {
        if (ac == null) {
            Log.e("siyehua-status", "Activity has been destory!");
            return;
        }
        View rootView = ac.findViewById(android.R.id.content);
        if (rootView != null && rootView instanceof FrameLayout) {
            FrameLayout rootGroup = (FrameLayout) rootView;
            View childView = rootGroup.findViewById(id);
            if (childView != null) {// 如果有这个View
                int centerY = childView.getBottom() - childView.getHeight() / 2;// 得到当前View的中心点Y
                int centerX = childView.getRight() - childView.getWidth() / 2;// 得到当前View的中心点X
                if (toastView == null) {
                    toastView = new TextView(ac);
                    toastView.setIncludeFontPadding(false);
                    toastView.setGravity(Gravity.CENTER);
                    rootGroup.addView(toastView);
                    FrameLayout.LayoutParams layoutParams = (LayoutParams) toastView
                            .getLayoutParams();
                    layoutParams.width = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
                    layoutParams.height = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
                    int textWidth = (int) toastView.getPaint().measureText(
                            content);// 得到字体宽度
                    Rect textBounds = new Rect();
                    toastView.getPaint().getTextBounds(content, 0,
                            content.length(), textBounds);
                    int textHeight = textBounds.bottom - textBounds.top;// 得到字体的高度
                    layoutParams.setMargins(centerX - textWidth / 2, centerY
                            - textHeight / 2, 0, 0);// 设置TextView的位置(中心与需要替换的View的中心一致)
                    toastView.setLayoutParams(layoutParams);
                }
                toastView.setText(content);
                toastView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
                toastView.setTextColor(textColor);
            } else {
                Log.e("siyehua-status",
                        "The view  isn't exist which you want to replaced! Please check the view's id.");
            }
        } else {
            Log.e("siyehua-status",
                    "The rootView isn't exist! Please check the rootView's id in code.");
        }
    }

    /**
     * @方法名: removeToastView
     * @功能描述:移除添加的文字View
     * @param ac
     *            当前Activity
     * @return void
     */
    public static void removeToastView(FragmentActivity ac) {
        if (ac == null) {
            Log.e("siyehua-status", "Activity has been destory!");
            return;
        }
        if (toastView != null) {
            View rootView = ac.findViewById(android.R.id.content);
            if (rootView != null && rootView instanceof FrameLayout) {
                FrameLayout rootGroup = (FrameLayout) rootView;
                rootGroup.removeView(toastView);
                toastView = null;
            } else {
                Log.e("siyehua-status",
                        "The rootView isn't exist! Please check the rootView's id in code.");
            }
        }
    }

不足:
当有多个提示内容时,使用的是同一个Textview
可以把静态方法和属性写成非静态的.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值