最近项目中遇到使用通知栏的功能,开始使用Notification的时候,发现存在兼容问题,于是换成了
WindowManager , 发现 WindowManager 能够很好的解决问题。
先看下效果图
以下是详细内容 ps:代码用的Kotlin
首先布局文件
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/windowFloatContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/layout_middle_margin"
android:clipChildren="false"
android:clipToPadding="false"
android:fitsSystemWindows="true"
app:cardBackgroundColor="@color/orange_bg"
app:cardCornerRadius="10dp"
app:contentPadding="@dimen/layout_middle_padding">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:lineSpacingExtra="5dp"
android:padding="@dimen/layout_normal_padding"
android:text="实时\n从茶园鲁能领秀城出发到光电园凤凰C座"
android:textColor="@color/white"
android:textSize="@dimen/middle_text_size"
tools:ignore="HardcodedText" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/layout_large_margin"
android:layout_marginRight="@dimen/layout_large_margin">
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="@color/white"
android:padding="@dimen/layout_small_padding"
android:textSize="@dimen/normal_text_size"
android:text="距您:0.86公里"/>
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:padding="@dimen/layout_small_padding"
android:textColor="@color/white"
android:textSize="@dimen/normal_text_size"
android:text="全程:18.65公里"/>
</FrameLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
其次 在Application中创建 WindowManager.LayoutParams
class App : Application() {
companion object {
private var wmParams: WindowManager.LayoutParams? = null
fun getWindowLayoutParams(): WindowManager.LayoutParams? = wmParams
}
override fun onCreate() {
super.onCreate()
wmParams = WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PRIORITY_PHONE,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT)
}
...
}
然后在Activity中使用
class MainActivity : BaseActivity() {
private var mWindowManager: WindowManager? = null
private var mWindowParams: WindowManager.LayoutParams? = null
private var floatView: View? = null
override fun getLayoutId(): Int = R.layout.activity_main
override fun initView() {
}
override fun onViewClick() {
super.onViewClick()
RxView.clicks(mainNotifyBtn)
.throttleFirst(2, TimeUnit.SECONDS)
.subscribeBy(onNext = {
showWindowAlert()
}, onError = {
})
RxView.clicks(mainCancelNotifyBtn)
.throttleFirst(2, TimeUnit.SECONDS)
.subscribeBy(onNext = {
hideWindowAlert()
}, onError = {
})
}
override fun isSupportBack(): Boolean = false
override fun getToolBarTitle(): String? = "San"
override fun processLogic() {
}
private fun showWindowAlert() {
mWindowManager = applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
mWindowParams = App.getWindowLayoutParams()
floatView = layoutInflater.inflate(R.layout.ic_window_float, null)
// mWindowParams!!.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
// mWindowParams!!.format = 1
// mWindowParams!!.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE // 不能抢占聚焦点
// mWindowParams!!.flags = mWindowParams!!.flags or WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
// mWindowParams!!.flags = mWindowParams!!.flags or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS // 排版不受限制
// mWindowParams!!.flags = mWindowParams!!.flags or WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS //
// mWindowParams!!.alpha = 0F
mWindowParams!!.gravity = Gravity.TOP
mWindowParams!!.x = 0
mWindowParams!!.y = 0
mWindowParams!!.verticalMargin = 0F
mWindowParams!!.horizontalMargin = 15F
// mWindowParams!!.width = floatView!!.layoutParams.width
// mWindowParams!!.height = floatView!!.layoutParams.height
mWindowManager!!.addView(floatView, mWindowParams)
}
private fun hideWindowAlert() {
if (mWindowManager != null)
mWindowManager!!.removeViewImmediate(floatView)
}
override fun onDestroy() {
super.onDestroy()
hideWindowAlert()
}
}
可以根据实际功能需求,给FloatView添加动画
按照上面的代码做,功能就能实现了。谢谢!