本文主要是介绍使用悬浮窗居中的问题 -- 有效果图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
今天写了个全局可以弹出的悬浮窗来显示重要提示,结果自己把自己绕进去了,一直居中不生效,最后恍然大悟,是全屏状态下背景是透明的,都全屏铺满了,还咋居中啊,解决方法是内部在套一层布局,随便指定位置即可 ,另外这里的文字可以自动滚动显示,下面是截图和关键代码:
效果图:
布局文件:关键是第二个RelativeLayout 的 android:layout_centerInParent="true" 调整这个RelativeLayout的位置 可以改变截图中
弹出框的位置
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/ll_root"android:layout_width="wrap_content"android:layout_height="wrap_content"><RelativeLayoutandroid:layout_centerInParent="true"android:layout_width="1030dp"android:layout_height="540dp"android:background="#000000"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="58dp"android:layout_marginTop="35dp"android:orientation="horizontal"><ImageViewandroid:layout_width="32dp"android:layout_height="32dp"android:background="@drawable/ic_launcher" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="12dp"android:text="紧急通知"android:textColor="#fd1963"android:textSize="24dp" /></LinearLayout><com.star.alarm.view.AutoScrollViewandroid:id="@+id/message_window_autoscrollview"android:layout_width="910dp"android:layout_height="390dp"android:layout_marginLeft="60dp"android:layout_marginTop="105dp"android:layout_marginRight="60dp"><TextViewandroid:id="@+id/message_window_textView"android:layout_width="910dp"android:layout_height="390dp"android:focusable="false"android:gravity="left"android:text="Hello World!\n\nHello World!\n\nHello World!\n\nHello World!\n\nHello World!\n\nHello World!\n\nHello World!\n\nHello World!\n\nHello World!\n\nHello World!\n\nHello World!"android:textColor="#f0f0f0"android:textSize="19dp" /></com.star.alarm.view.AutoScrollView></RelativeLayout></RelativeLayout>
Window 窗口相关:
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;import com.star.alarm.R;
import com.star.alarm.constant.AlarmConstant;
import com.star.alarm.dto.MessageDTO;
import com.star.alarm.util.WindowHelper;
import com.star.star_common.StarCommon;
import com.star.star_common.constants.StarCoreVariable;
import com.star.star_common.util.LogUtil;
import com.star.star_common.util.ToastUtil;import java.util.List;import static android.content.Context.WINDOW_SERVICE;public class MessageWindowManager {private static volatile MessageWindowManager instance;private WindowManager windowManager;private View view;private AutoScrollView message_window_autoscrollview;private TextView message_window_textView;private MessageWindowManager() {}public static MessageWindowManager getInstance() {if (instance == null) {synchronized (MessageWindowManager.class) {if (instance == null) {instance = new MessageWindowManager();}}}return instance;}/*** 隐藏消息窗口*/public void hide() {if (view != null && windowManager != null) {try {windowManager.removeView(view);} catch (Exception e) {e.printStackTrace();}}}/*** 展示消息窗口*/public void show(Context context) {hide(); windowManager = (WindowManager)context.getSystemService(WINDOW_SERVICE);initUI(context);}/** 初始化控件** */private void initUI(Context context) {try {view = LayoutInflater.from(context).inflate(R.layout.message_window, null);message_window_autoscrollview = view.findViewById(R.id.message_window_autoscrollview);message_window_textView = view.findViewById(R.id.message_window_textView);message_window_autoscrollview.setAutoToScroll(true);//设置可以自动滑动message_window_autoscrollview.setFistTimeScroll(2000);//设置第一次自动滑动的时间message_window_autoscrollview.setScrollRate(200);//设置滑动的速率message_window_autoscrollview.setScrollLoop(true);//设置是否循环滑动message_window_autoscrollview.setScanScrollChangedListener(new AutoScrollView.ISmartScrollChangedListener() {@Overridepublic void onScrolledToBottom() {ToastUtil.showShortToast("底部");}@Overridepublic void onScrolledToTop() {ToastUtil.showShortToast("顶部");}});//窗口管理WindowHelper windowHelper = new WindowHelper();windowManager.addView(view, windowHelper.getParams(context));} catch (Exception e) {e.printStackTrace();}}
}
WindowHelper类:
import android.graphics.PixelFormat;
import android.view.WindowManager;public class WindowHelper {public WindowManager.LayoutParams getParams(Context context) {// 窗体的布局样式WindowManager.LayoutParams mLayout = new WindowManager.LayoutParams();// 设置窗体显示类型——TYPE_SYSTEM_ALERT(系统提示)mLayout.type = WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;// 设置窗体焦点及触摸:// FLAG_NOT_FOCUSABLE(不能获得按键输入焦点)mLayout.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;// 设置显示的模式mLayout.format = PixelFormat.TRANSLUCENT;// 设置窗体宽度和高度mLayout.width = WindowManager.LayoutParams.MATCH_PARENT;mLayout.height = WindowManager.LayoutParams.WRAP_CONTENT;//调用此方法会使机顶盒卡死
// mLayout.horizontalMargin = DensityUtil.dip2px(context, R.dimen.adaptation_60px);return mLayout;}
}
AutoScrollView:
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;/*** 自动滚动textview的封装* 监听ScrollView滚动到顶部或者底部做相关事件拦截*/
public class AutoScrollView extends ScrollView {private boolean isScrolledToTop = true; // 初始化的时候设置一下值private boolean isScrolledToBottom = false;private int paddingTop = 0;private final int MSG_SCROLL = 10;private final int MSG_SCROLL_Loop = 11;private boolean scrollAble = false;//是否能滑动//三个可设置的属性private boolean autoToScroll = true; //是否自动滚动private boolean scrollLoop = false; //是否循环滚动private int fistTimeScroll = 5000;//多少秒后开始滚动,默认5秒private int scrollRate = 50;//多少毫秒滚动一个像素点public AutoScrollView(Context context) {this(context, null);}public AutoScrollView(Context context, AttributeSet attrs) {super(context, attrs, 0);}private ISmartScrollChangedListener mSmartScrollChangedListener;/*** 定义监听接口*/public interface ISmartScrollChangedListener {void onScrolledToBottom(); //滑动到底部void onScrolledToTop();//滑动到顶部}//设置滑动到顶部或底部的监听public void setScanScrollChangedListener(ISmartScrollChangedListener smartScrollChangedListener) {mSmartScrollChangedListener = smartScrollChangedListener;}//ScrollView内的视图进行滑动时的回调方法,据说是API 9后都是调用这个方法,但是我测试过并不准确@Overrideprotected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);if (scrollY == 0) {isScrolledToTop = clampedY;isScrolledToBottom = false;} else {isScrolledToTop = false;isScrolledToBottom = clampedY;//系统回调告诉你什么时候滑动到底部}notifyScrollChangedListeners();}int lastY;//触摸事件@Overridepublic boolean onTouchEvent(MotionEvent event) {int y = (int) event.getY();switch (event.getAction()) {case MotionEvent.ACTION_DOWN:// 记录触摸点坐标lastY = y;break;case MotionEvent.ACTION_MOVE:// 计算偏移量int offsetY = y - lastY;// 在当前left、top、right、bottom的基础上加上偏移量paddingTop = paddingTop - offsetY / 10;//不要问我上面10怎么来的,我大概估算的,正常一点应该是7或8吧,我故意让手动滑动的时候少一丢scrollTo(0, paddingTop);break;}return true;}//ScrollView内的视图进行滑动时的回调方法,据说是API 9前都是调用这个方法,我新版的SDK也是或回调这个方法@Overrideprotected void onScrollChanged(int l, int t, int oldl, int oldt) {super.onScrollChanged(l, t, oldl, oldt);// if (android.os.Build.VERSION.SDK_INT < 9) { // API 9及之后走onOverScrolled方法监听,if (getScrollY() == 0) {isScrolledToTop = true;isScrolledToBottom = false;} else if (getScrollY() + getHeight() - getPaddingTop() - getPaddingBottom() == getChildAt(0).getHeight()) {isScrolledToBottom = true;isScrolledToTop = false;} else {isScrolledToTop = false;isScrolledToBottom = false;}notifyScrollChangedListeners();
// }}//判断是否滑动到底部或顶部private void notifyScrollChangedListeners() {if (isScrolledToTop) {if (mSmartScrollChangedListener != null) {mSmartScrollChangedListener.onScrolledToTop();}} else if (isScrolledToBottom) {mHandler.removeMessages(MSG_SCROLL);if (!scrollLoop) {scrollAble = false;}if (scrollLoop) {mHandler.sendEmptyMessageDelayed(MSG_SCROLL_Loop, fistTimeScroll);}if (mSmartScrollChangedListener != null) {mSmartScrollChangedListener.onScrolledToBottom();}}}//handlerprivate Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);switch (msg.what) {case MSG_SCROLL:if (scrollAble && autoToScroll) {scrollTo(0, paddingTop);paddingTop += 1;mHandler.removeMessages(MSG_SCROLL);mHandler.sendEmptyMessageDelayed(MSG_SCROLL, scrollRate);}break;case MSG_SCROLL_Loop:paddingTop = 0;autoToScroll = true;mHandler.sendEmptyMessageDelayed(MSG_SCROLL, fistTimeScroll);}}};//获取子View和ScrollView的高度比较,决定是否能够滑动View@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);View childAt = getChildAt(0);int childMeasuredHeight = childAt.getMeasuredHeight(); //获取子控件的高度int measuredHeight = getMeasuredHeight();//获取ScrollView的高度
// Log.e("onMeasure", "childMeasuredHeight:" + childMeasuredHeight + " ,measuredHeight:" + measuredHeight);if (childMeasuredHeight > measuredHeight) { //如果子控件的高度大于父控件才需要滚动scrollAble = true;paddingTop = 0;mHandler.sendEmptyMessageDelayed(MSG_SCROLL, fistTimeScroll);} else {scrollAble = false;}}//设置是否自动滚动public void setAutoToScroll(boolean autoToScroll) {this.autoToScroll = autoToScroll;}//设置第一次开始滚动的时间public void setFistTimeScroll(int fistTimeScroll) {this.fistTimeScroll = fistTimeScroll;mHandler.removeMessages(MSG_SCROLL);mHandler.sendEmptyMessageDelayed(MSG_SCROLL, fistTimeScroll);}//设置滚动的速率,多少毫秒滚动一个像素点public void setScrollRate(int scrollRate) {this.scrollRate = scrollRate;}//设置是否循环滚动public void setScrollLoop(boolean scrollLoop) {this.scrollLoop = scrollLoop;}}
最后在希望弹出窗的位置调用:
MessageWindowManager.getInstance().show("传入context 可以是application的");
即可弹出窗。
这篇关于使用悬浮窗居中的问题 -- 有效果图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!