【问题标题】:Setting AlertDialog width to fit the size of the fixed size imageview using builder.setView()使用 builder.setView() 设置 AlertDialog 宽度以适合固定大小的 imageview 的大小
【发布时间】:2019-08-26 12:12:46
【问题描述】:

我是 android 开发新手,我想设置 AlertDialog 以显示一个固定大小宽度 = 150dp,高度 = 150dp 的图像视图。但是,当图像小于父宽度时,它具有我想要处理的背景空间。

我试过dialog.setView(dialogview,0,0,0,0).create() 不起作用,父布局和imageview 布局的宽度和高度的多个布局更改,不起作用。 设置dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)),不起作用和其他各种东西。

这是我的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="horizontal"
          android:background="#44403c"
          android:layout_width="match_parent"
          android:gravity="center"
          android:layout_height="match_parent"
android:layout_gravity="center">
<pl.droidsonroids.gif.GifImageView 
android:layout_width="150dp" android:layout_height="160dp"                                 
android:src="@drawable/tickgifcrop"                                     
android:scaleType="centerCrop"/>

这是我的 kotlin 代码:

val builder = AlertDialog.Builder(this)
val progressdialogview = LayoutInflater.from(this).inflate(R.layout.successdialog,null)
builder.setCancelable(false)
builder.setView(progressdialogview)
val dialog = builder.create()
dialog.show()
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

This is what it look like.

【问题讨论】:

    标签: java android android-layout kotlin


    【解决方案1】:

    改变父布局大小为wrap_content

    <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="horizontal"
                  android:background="@android:color/transparent"
                  android:layout_width="wrap_content"
                  android:gravity="center"
                  android:layout_height="wrap_content"
        android:layout_gravity="center">
        <pl.droidsonroids.gif.GifImageView 
        android:layout_width="150dp" android:layout_height="160dp"                                 
        android:src="@drawable/tickgifcrop"                                     
        android:scaleType="centerCrop"/>
    

    并在dialog.show()之后添加以下内容到您的代码中

    val lp = WindowManager.LayoutParams()
            lp.copyFrom(dialog.window?.attributes)
            lp.width = WindowManager.LayoutParams.WRAP_CONTENT
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT
    
           dialog.window?.attributes = lp
    

    删除对话框后面的阴影添加

    dialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
    

    【讨论】:

    • 一件事,让你的父视图透明 android:background="@android:color/transparent"
    猜你喜欢
    • 2017-03-24
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 2020-02-10
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多