前言
現(xiàn)在基本上彈窗都用 DialogFragment 來寫了。
其優(yōu)勢很明顯(比如擁有生命周期&旋轉(zhuǎn)屏幕不消失等)炊汤,用起來方便
基本可以淘汰 Dialog 了正驻。
需求
彈窗
實(shí)現(xiàn)
實(shí)現(xiàn)效果
我還是按照之前使用 PopupWindow 的方式寫的布局:
父層撐滿弊攘,內(nèi)容區(qū)居中,并在父層設(shè)置半透明遮罩姑曙。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_50_000000"
>
<RelativeLayout
android:id="@+id/rl_dialog_task_main"
android:layout_width="@dimen/dim280"
android:layout_height="@dimen/dim204"
android:background="@color/transparent"
android:layout_centerInParent="true">
...
...
</RelativeLayout>
</RelativeLayout>
但是襟交,DialogFragment 的背景色是默認(rèn)設(shè)置的,我設(shè)置的背景色伤靠,只在內(nèi)容區(qū)顯示
如上圖捣域,有棱角的半透明黑色,同時因?yàn)椴灰?guī)則宴合,不能用圓角背景解決焕梅。
解決方案
- 在 onCreateView 中添加 下面兩句話,即可
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
- 同時在Xml文件中卦洽,不設(shè)置背景色
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- android:background="@color/color_50_000000"-->
實(shí)現(xiàn)效果
實(shí)現(xiàn)效果
附相關(guān)代碼
/**
* <pre>
* author : jake
* time : 2018/09/07
* function : 任務(wù)詳情頁彈窗 通用類型
* version: 1.0
* </pre>
*/
abstract class TaskDialog : AppCompatDialogFragment() {
abstract val tips: String
abstract val function: String
private var functionCallback: (() -> Unit)? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
val view = inflater.inflate(R.layout.dialog_task, container, false)
view.tv_dialog_tips.text = tips
view.tv_dialog_fun.text = function
view.tv_dialog_cancel.setOnClickListener {
dismiss()
}
view.tv_dialog_fun.setOnClickListener {
dismiss()
functionCallback?.invoke()
}
return view
}
fun show(manager: FragmentManager?, callback: () -> Unit) {
super.show(manager, tips)
this.functionCallback = callback
}
}
想要設(shè)置不可取消丘侠,點(diǎn)擊空白區(qū)域也不可取消,可以添加如下屬性:
// 設(shè)置點(diǎn)擊其他區(qū)域不可取消
dialog.setCancelable(false)
dialog.setCanceledOnTouchOutside(false)
雖然不可取消逐样,但是返回鍵仍能使其消失蜗字,所以要監(jiān)聽 返回鍵,并攔截點(diǎn)擊事件
// 監(jiān)聽返回鍵
dialog.setOnKeyListener(DialogInterface.OnKeyListener { _, p1, _ ->
if (p1 == KeyEvent.KEYCODE_BACK) {
return@OnKeyListener true
}
false
})
子類
/**
* <pre>
* author : jake
* time : 2018/09/07
* function : 任務(wù)詳情頁彈窗 刪除錄音
* version: 1.0
* </pre>
*/
class TaskDeleteDialog : TaskDialog() {
override val tips: String
get() = stringForRes(R.string.delete_confirm)
override val function: String
get() = stringForRes(R.string.delete)
}
使用
private val deleteDialog by lazy { TaskDeleteDialog() }
// 設(shè)置返回點(diǎn)擊監(jiān)聽
iv_task_back.setOnClickListener {
deleteDialog.show(fragmentManager){
activity!!.finish()
}
}
注意脂新,當(dāng)dialog實(shí)例化兩次時挪捕,會報錯閃退,所以在有可能調(diào)用兩次的地方争便,添加是否實(shí)例化判斷
使用 isAdded 方法判斷即可
if (!reloginDialog.isAdded) {
reloginDialog.show((ActivityStack.currentActivity() as? AppCompatActivity)?.supportFragmentManager) {
loginOut()
}
}
設(shè)置BottomSheetDialogFragment背景透明(2020.3.3)
設(shè)置如下代碼竟然無效:
dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
改為如下代碼即可:
(view?.parent as? View)?.setBackgroundResource(R.color.transparent)
附部分代碼:
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return LayoutInflater.from(context).inflate(R.layout.dialog_select_course, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
// dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
(view?.parent as? View)?.setBackgroundResource(R.color.transparent)
text_title.typeface = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD)