首页 > 解决方案 > 使用 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 函数。

任何帮助,将不胜感激!

标签: androidkotlin

解决方案


推荐阅读