android - 使用 FrameLayout 创建 DialogFragment
问题描述
我被要求这样做,但我不知道这些是如何工作的,我尝试阅读文档,但它并不真正适合这个特定需求?
我的活动收到通知,解析 $title 和 $message 并使用此信息膨胀此 DialogFragment。
我创建了相关的 XML 文件并将其加载到这个预定义的FullScreenMessageView: FrameLayout
class FullScreenMessageView: FrameLayout, View.OnTouchListener {
constructor(context: Context) : super(context) { this.init(context) }
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { this.init(context) }
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { this.init(context) }
private fun init(context: Context) {
inflate(context, R.layout.full_screen_notification_loader, this)
setOnTouchListener(this)
}
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
return true
}
fun setTitle(title: String) {
//TODO: Adding title to XML view
}
fun setMessage(message: String){
//TODO: Adding message to XML view
}
}
我也有这个带有很多样板代码的 DialogFragment:
class FullScreenLoaderDialog: DialogFragment() {
private lateinit var message: String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
message = arguments!!.getString(MESSAGE_PARAM, DEFAULT_LOADING_MESSAGE)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
super.onCreateView(inflater, container, savedInstanceState)
return FullScreenLoader(this.context!!)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
this.dialog!!.window.apply {
requestFeature(Window.FEATURE_NO_TITLE)
setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
val width = WindowManager.LayoutParams.MATCH_PARENT
val height = WindowManager.LayoutParams.MATCH_PARENT
setLayout(width, height)
}
(this.view as FullScreenLoader).setLoadingText(message)
this.view!!.setOnClickListener { this.dismiss() }
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return Dialog(activity!!, R.style.AppTheme_LoadingDialog)
}
companion object{
private const val DEFAULT_LOADING_MESSAGE = "Loading"
private const val MESSAGE_PARAM="MESSAGE_PARAM"
fun newInstance(message: String) = FullScreenLoaderDialog().apply {
arguments = Bundle().apply {
putString(MESSAGE_PARAM, message)
}
}
}
}
我知道如何从我的活动中膨胀这个片段,但不确定如何将这个自定义视图连接到 DialogFragment。而且我也不确定如何将标题添加到 XML 视图或在哪里调用这些 setTitle 和 setMessage 函数。
任何帮助,将不胜感激!
解决方案
推荐阅读
- java - 通过流式传输 Pubsub 数据丰富固定 BigQuery 数据的数据流模板/模式
- javascript - 使用javascript复制CSS:多个元素的焦点选择器
- flutter - 我们可以使用使用flutter_blue包的蓝牙热敏打印机吗
- inno-setup - 使用 Inno Set Up 编译器,是否可以在 finalize 页面上同时混合 Checkbox 和 Radio 按钮?
- ios - 我无法通过 Flutter 将 http 响应解码为 UTF-8
- photoshop - Photoshop中的脚本测量工具来测量两点之间的距离
- javascript - 如何在同一个函数中重复函数?
- webpack - handlebars-helpers /node_modules/log-utils/ 无法静态分析 'require(..., ...)' webpack
- redirect - Apache2:从自定义链接重定向到 html 文件
- mongodb - 如何从 mongo 管道中检索每个单个数组元素?