Android顶部弹出提示语的三种实现方式:WindowManager、PopupWindow、Toast

阅读: 评论:0

Android顶部弹出提示语的三种实现方式:WindowManager、PopupWindow、Toast

Android顶部弹出提示语的三种实现方式:WindowManager、PopupWindow、Toast

需求:在主页Activity顶部从上向下滑动出现提示条,且5秒后自动从下向上滑动消失。

自定义布局文件:l

// l
<LinearLayoutandroid:layout_width="match_parent"android:layout_height="120px"android:paddingStart="20px"android:paddingEnd="20px"android:gravity="center"android:background="#cc000000"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="30px"android:layout_marginBottom="30px"android:text="提示语"android:textSize="38px"android:textColor="@android:color/white"/>
</LinearLayout>

自定义滑动动画:

<style name="popwindowAnimStyle" parent="android:Animation"><item name="android:windowEnterAnimation">@anim/anim_popup_show</item><item name="android:windowExitAnimation">@anim/anim_popup_exit</item>
</style>

自定义出现动画:anim_l

<set xmlns:android=""><translateandroid:fromYDelta="-120%"android:toYDelta="0"android:duration="400"/>
</set>

自定义消失动画:anim_l

<set xmlns:android=""><translateandroid:fromYDelta="0"android:toYDelta="-120%"android:duration="400"/>
</set>

一、WindowManager实现

优点:可始终显示在当前Activity内所有Fragment、Dialog之上;
缺点:若切换Activity,则被新Activity覆盖。

View contentView = LayoutInflater.from(this).inflate(R.layout.layout_tips, null);
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
if(windowManager != null) {WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |	//不拦截页面点击事件WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;layoutParams.format = PixelFormat.avity = Gravity.TOP;layoutParams.y = (int) getResources().getDimension(R.dimen.y30);layoutParams.height = (int) getResources().getDimension(R.dimen.y119);layoutParams.windowAnimations = R.style.popwindowAnimStyle;	//自定义动画windowManager.addView(contentView, layoutParams);new Handler().postDelayed(new Runnable() {@Overridepublic void run() {if(windowManager != null) {veViewImmediate(contentView);windowManager = null;}}}, 3000);
}

二、PopupWindow实现

缺点:会被当前Activity新出现的Fragment、Dialog覆盖。

View contentView = LayoutInflater.from(this).inflate(R.layout.layout_tips, null);
asure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
DisplayMetrics dm = getResources().getDisplayMetrics();
PopupWindow popupWindow = new PopupWindow(contentView, dm.widthPixels - PaddingStart() * 2,(int) getResources().getDimension(R.dimen.y120));
popupWindow.setOutsideTouchable(false);	//点击外部区域不消失
popupWindow.setTouchable(false);
popupWindow.setBackgroundDrawable(null);
popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
popupWindow.setAnimationStyle(R.style.popwindowAnimStyle);	//自定义动画
popupWindow.showAtLocation(tvShopDeviceName, Gravity.TOP | Gravity.CENTER_HORIZONTAL,0, (int) getResources().getDimension(R.dimen.y30));	//指定顶部位置new Handler().postDelayed(new Runnable() {@Overridepublic void run() {popupWindow.dismiss();}
}, 3000);

三、Toast实现

优点:可始终显示在屏幕最上层,不被新Activity覆盖;
缺点:Android 7.0后无法自定义显示/隐藏动画,默认为渐隐渐现。

View contentView = LayoutInflater.from(this).inflate(R.layout.layout_tips, null);
Toast videoToast = Toast.makeText(this, "", Toast.LENGTH_LONG);
videoToast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, (int) getResources().getDimension(R.dimen.y30));
videoToast.setView(contentView);
try {Object mTN = getField(videoToast, "mTN");if(mTN != null) {Object mParams = getField(mTN, "mParams");if(mParams instanceof WindowManager.LayoutParams) {WindowManager.LayoutParams params = (WindowManager.LayoutParams) mParams;//params.windowAnimations = R.style.popwindowAnimStyle;	//自定义动画无效params.width = dm.widthPixels - PaddingStart() * 2;params.height = (int) getResources().getDimension(R.dimen.y120);}}
} catch (Exception e){e.printStackTrace();
}
videoToast.show();private Object getField(Object object, String fieldName)throws NoSuchFieldException, IllegalAccessException{Field field = Class().getDeclaredField(fieldName);if(field != null) {field.setAccessible(true);(object);}return null;
}

本文发布于:2024-01-28 20:24:00,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170644464310049.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:弹出   三种   提示   方式   Android
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23