razerdp / BasePopup

Android下打造通用便捷的PopupWindow弹窗库
https://github.com/razerdp/BasePopup
Apache License 2.0
5.2k stars 759 forks source link

Activity主题设置为常规,弹窗xml中宽度高度 设置无效,唉,不是所见即所得吗 #477

Closed pichsy closed 1 year ago

razerdp commented 2 years ago

上代码

pichsy commented 2 years ago

这么写布局 宽度和高度就不对,所见不一定为所得,特别是用约束布局时ConstraintLayout

 class IdiomsHeroPopup(var context: Context, var message: String?) : BasePopupWindow(context) {

     private var binding: PopupIdiomsHeroBinding =
           PopupIdiomsHeroBinding.inflate(LayoutInflater.from(context))

     init {
            contentView = binding.root
    }
pichsy commented 2 years ago

我知道原因了,你没兼容 setContentView(view)只兼容了 setContentView(resId)

pichsy commented 2 years ago

所以我的viewbinding不能直接用

要这么写:

private lateinit var binding: PopupIdiomsTipsBinding

init {
    setContentView(R.layout.popup_idioms_tips)
}

override fun onViewCreated(contentView: View) {
    binding = PopupIdiomsTipsBinding.bind(contentView)
pichsy commented 2 years ago

作者大大, 啥时候兼容一下

这么写就完美了,符合开发习惯

 private var binding: PopupIdiomsHeroBinding =
 PopupIdiomsHeroBinding.inflate(LayoutInflater.from(context))
 init {
        contentView = binding.root
  }
razerdp commented 2 years ago

view是无法兼容的,因为view要么是new要么是inflate,在这之后我就没机会读取xml中的配置了。 之所以能兼容id是因为在inflate的时候我创建了一个临时的framelayout,借此读取xml中的设置(即layoutparam)

事实上对于viewbinding等依赖注入库我也提供了文档指引: https://www.yuque.com/razerdp/basepopup/iopg16

pichsy commented 2 years ago

@NonNull public ConstraintLayout getRoot() { return this.rootView; }

@NonNull
public static PopupIdiomsTipsBinding inflate(@NonNull LayoutInflater inflater) {
    return inflate(inflater, (ViewGroup)null, false);
}

---->>这里可能有突破口,用这个方法创建,可以传入一个parent, 封装封装,是否能达到想要的?

@NonNull
public static PopupIdiomsTipsBinding inflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup parent, boolean attachToParent) {
    View root = inflater.inflate(layout.popup_idioms_tips, parent, false);
    if (attachToParent) {
        parent.addView(root);
    }

    return bind(root);
}
razerdp commented 2 years ago

xml的属性是inflate方法里赋予的,后期再addview是无效的= = 如果有别的方法读取到xml属性就好了,这个问题到目前为止我还没找到方法。。。

razerdp commented 1 year ago

暂时无解~如果有方法读取init后的view对应xml中的gravity,欢迎在这里留下idea~