Picutre 未显示在警报对话框中



我使用PhotoView实现缩放功能。幸运的是,我的应用程序能够从给定的uri中获取图片,但是当它要显示它时,它只显示一个透明的框。

image_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.github.chrisbanes.photoview.PhotoView
android:id="@+id/zoom_iv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

Kotlin代码实现的功能:

... 
binding.image.setOnClickListener(){
ImageLayoutDialog()
}
...
...
...
private fun ImageLayoutDialog() {
val imageLayoutBinding =
ImageLayoutBinding.inflate(LayoutInflater.from(requireContext()))
val photoView: PhotoView = imageDialogBinding.zoomIv
val mBuilder = AlertDialog.Builder(requireContext())
.setView(
imageLayoutBinding .root
)
Glide.with(requireActivity())
.load(customUri)
.into(photoView)
val mAlertDialog : AlertDialog =  mBuilder.create()
mAlertDialog.show()
mAlertDialog.setCanceledOnTouchOutside(false)
}

当我点击image时,我得到一个透明的屏幕。但是我期待的是customUri的内容。有什么解决办法吗?

编辑1.0

现在对话框加载。但是在设置视图之后,它不会显示图像。

在这种情况下我建议您使用其他类型的对话框。比如DialogFragment,或者AppCompatDialog。

解决方案使用AppCompatDialog(我认为这将是最类似于你的需要):

class ImageDialog (private var context: Context, private var yourUri: Uri) : AppCompatDialog(context) {
private var _binding: DialogBinding? = null
private val binding get() = _binding!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val layoutInflater = LayoutInflater.from(context)
_binding = DialogBinding.inflate(layoutInflater)
setContentView(binding.root)
//here you can load your Uri with Glide.
}

}

实例化AppCompatDialog:

val imageDialog = ImageDialog(requireContext(), yourUri)
imageDialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
imageDialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
navigateToChangeUserAddressDialog.show()

我知道这不是一个警告对话框,但我正试图找到一个解决方案:D

你总是可以在对话框中添加个性化的按钮,当它被点击时,你可以使用imageDialog.cancel()来取消它,或者如果你想在视图中执行一些逻辑,你也可以传递一个lambda。

最新更新