需求:在主页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>
优点:可始终显示在当前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);
}
缺点:会被当前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);
优点:可始终显示在屏幕最上层,不被新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小时内删除。
留言与评论(共有 0 条评论) |