Android AlertDialog的使用

AlertDialog 的介紹

  1. AlertDialog的方法
返回值 方法名
open Button! getButton(whichButton: Int) 獲獲取對話框中使用的按鈕之一。如果指定的按鈕不存在或?qū)υ捒蛏形赐耆珓?chuàng)建(例如,通過 show() 或 create()),則返回 null。取dialog中某個(gè)按鈕, whichButton 可選的值: DialogInterface.BUTTON_POSITIVEDialogInterface.BUTTON_NEGATIVEDialogInterface.BUTTON_NEUTRAL(這是最左側(cè)的按鈕)
open ListView! getListView() 獲取對話框中使用的列表視圖。
open Boolean onKeyDown(keyCode: Int, event: KeyEvent) 按下某個(gè)鍵
open Boolean onKeyUp(keyCode: Int, event: KeyEvent) 抬起某個(gè)鍵
open Unit setButton(whichButton: Int, text: CharSequence!, msg: Message!)設(shè)置按下按鈕時(shí)要發(fā)送的消息。如果在 show() 之后調(diào)用此方法則無效。
open Unit setButton(whichButton: Int, text: CharSequence!, listener: OnClickListener!)設(shè)置在按下對話框的正按鈕時(shí)要調(diào)用的偵聽器。如果在 show() 之后調(diào)用此方法則無效。
open Unit setButton(whichButton: Int, text: CharSequence!, icon: Drawable!, listener: OnClickListener!)設(shè)置要與按鈕文本一起顯示的圖標(biāo)以及在按下對話框的正按鈕時(shí)要調(diào)用的偵聽器。如果在 show() 之后調(diào)用此方法則無效。
open Unit setCustomTitle(customTitleView: View!)設(shè)置自定義的title 如果在 show() 之后調(diào)用此方法則無效。
open Unit setIcon(resId: Int) 設(shè)置icon,如果不想要icon將resId設(shè)置為0
open Unit setIcon(icon: Drawable!) 設(shè)置要在標(biāo)題中使用的 Drawable。
open Unit setIconAttribute(attrId: Int)設(shè)置由主題屬性提供的圖標(biāo)。例如android.R.attr.alertDialogIcon
open Unit setMessage(message: CharSequence!)設(shè)置要顯示的消息。
open Unit setTitle(title: CharSequence?) 設(shè)置標(biāo)題
open Unit setView(view: View!)設(shè)置要在對話框中顯示的視圖。如果在 show() 之后調(diào)用此方法則無效。
open Unit setView(view: View!, viewSpacingLeft: Int, viewSpacingTop: Int, viewSpacingRight: Int, viewSpacingBottom: Int)設(shè)置要在對話框中顯示的視圖,指定在該視圖周圍顯示的間距。如果在 show() 之后調(diào)用此方法則無效。
  1. AlertDialog的創(chuàng)建

AlertDialog的構(gòu)造函數(shù)都是protected的,因此我們無法直接通過構(gòu)造函數(shù)實(shí)例化,所以SDK提供了 AlertDialog.Builder這個(gè)內(nèi)部類用來創(chuàng)建AlertDialog

構(gòu)造方法
Builder(context: Context!) 為使用默認(rèn)警報(bào)對話框主題的警報(bào)對話框創(chuàng)建構(gòu)建器。 默認(rèn)警報(bào)對話框主題由父上下文主題中的 R.attr.alertDialogTheme 定義。
Builder(context: Context!, themeResId: Int) 為使用顯式主題資源的警報(bào)對話框創(chuàng)建構(gòu)建器。 指定的主題資源 (themeResId) 應(yīng)用于父上下文的主題之上。它可以指定為包含完全填充主題的樣式資源,例如 R.style.Theme_Material_Dialog,以替換父上下文主題中的所有屬性,包括主色和強(qiáng)調(diào)色。 為了保留諸如主色和強(qiáng)調(diào)色之類的屬性,themeResId 可以改為指定為覆蓋主題,例如 R.style.ThemeOverlay_Material_Dialog。這將僅覆蓋將警報(bào)窗口樣式化為對話框所需的窗口屬性。 或者,可以將 themeResId 指定為 0 以使用 R.attr.alertDialogTheme 的父上下文解析值。
返回值 方法名
open AlertDialog! create()使用提供給此構(gòu)建器的參數(shù)創(chuàng)建 AlertDialog。 調(diào)用此方法不會(huì)顯示對話框。如果不需要額外的處理,可以調(diào)用 show() 來創(chuàng)建和顯示對話框。
open Context! getContext()為由此生成器創(chuàng)建的對話框返回具有適當(dāng)主題的上下文。應(yīng)用程序應(yīng)該使用這個(gè) Context 來獲取 LayoutInflaters 來膨脹將在結(jié)果對話框中使用的視圖,因?yàn)樗鼤?huì)導(dǎo)致視圖用正確的主題膨脹。
open AlertDialog.Builder! setAdapter(adapter: ListAdapter!, listener: DialogInterface.OnClickListener!)設(shè)置由給定 ListAdapter 提供的項(xiàng)目列表作為內(nèi)容顯示在對話框中,您將通過提供的偵聽器收到所選項(xiàng)目的通知。
open AlertDialog.Builder! setCancelable(cancelable: Boolean)設(shè)置對話框是否可取消。默認(rèn)為true。
open AlertDialog.Builder! setCursor(cursor: Cursor!, listener: DialogInterface.OnClickListener!, labelColumn: String!)設(shè)置由給定 Cursor 提供的項(xiàng)目列表,作為內(nèi)容顯示在對話框中,您將通過提供的偵聽器收到所選項(xiàng)目的通知。
open AlertDialog.Builder! setCustomTitle(customTitleView: View!)使用自定義視圖 customTitleView 設(shè)置標(biāo)題。 方法 setTitle(int) 和 setIcon(int) 應(yīng)該足以滿足大多數(shù)標(biāo)題,但如果標(biāo)題需要更多自定義,則提供此方法。使用這將替換通過其他方法設(shè)置的標(biāo)題和圖標(biāo)。 注意:為了確保樣式一致,自定義視圖應(yīng)該使用通過 getContext() 獲得的警報(bào)對話框的主題上下文來膨脹或構(gòu)造。
open AlertDialog.Builder! setIcon(iconId: Int)設(shè)置要在標(biāo)題中使用的 Drawable 的資源 ID。 優(yōu)先于使用 setIcon(android.graphics.drawable.Drawable) 設(shè)置的值
open AlertDialog.Builder! setIcon(icon: Drawable!)設(shè)置要在標(biāo)題中使用的 Drawable。 注意:為確保樣式一致,應(yīng)使用通過 getContext() 獲取的警報(bào)對話框的主題上下文來擴(kuò)充或構(gòu)造可繪制對象。
open AlertDialog.Builder! setIconAttribute(attrId: Int)設(shè)置由主題屬性提供的圖標(biāo)。例如android.R.attr#alertDialogIcon。 優(yōu)先于使用 setIcon(int) 或 setIcon(android.graphics.drawable.Drawable) 設(shè)置的值
open AlertDialog.Builder! setInverseBackgroundForced(useInverseBackground: Boolean)將警報(bào)對話框設(shè)置為使用反向背景,無論內(nèi)容是什么。(已棄用:此標(biāo)志僅用于之前的 Material主題。相反,使用警報(bào)對話框主題指定窗口背景。)
open AlertDialog.Builder! setItems(itemsId: Int, listener: DialogInterface.OnClickListener!)設(shè)置要在對話框中顯示的項(xiàng)目列表作為內(nèi)容,您將通過提供的偵聽器收到所選項(xiàng)目的通知。這應(yīng)該是一個(gè)數(shù)組類型,即 R.array.foo
open AlertDialog.Builder! setItems(items: Array<CharSequence!>!, listener: DialogInterface.OnClickListener!)設(shè)置要在對話框中顯示的項(xiàng)目列表作為內(nèi)容,您將通過提供的偵聽器收到所選項(xiàng)目的通知。
open AlertDialog.Builder! setMessage(messageId: Int) 使用給定的資源 ID 設(shè)置要顯示的消息。
open AlertDialog.Builder! setMessage(message: CharSequence!)設(shè)置要顯示的消息。
open AlertDialog.Builder! setMultiChoiceItems(itemsId: Int, checkedItems: BooleanArray!, listener: DialogInterface.OnMultiChoiceClickListener!)設(shè)置要在對話框中顯示的項(xiàng)目列表作為內(nèi)容,您將通過提供的偵聽器收到所選項(xiàng)目的通知。這應(yīng)該是一個(gè)數(shù)組類型,例如R.array.foo。該列表將在每個(gè)選中項(xiàng)目的文本右側(cè)顯示一個(gè)復(fù)選標(biāo)記。單擊列表中的項(xiàng)目不會(huì)關(guān)閉對話框。單擊按鈕將關(guān)閉對話框。
open AlertDialog.Builder! setMultiChoiceItems(items: Array<CharSequence!>!, checkedItems: BooleanArray!, listener: DialogInterface.OnMultiChoiceClickListener!)設(shè)置要在對話框中顯示的項(xiàng)目列表作為內(nèi)容,您將通過提供的偵聽器收到所選項(xiàng)目的通知。該列表將在每個(gè)選中項(xiàng)目的文本右側(cè)顯示一個(gè)復(fù)選標(biāo)記。單擊列表中的項(xiàng)目不會(huì)關(guān)閉對話框。單擊按鈕將關(guān)閉對話框。
open AlertDialog.Builder! setMultiChoiceItems(cursor: Cursor!, isCheckedColumn: String!, labelColumn: String!, listener: DialogInterface.OnMultiChoiceClickListener!)設(shè)置要在對話框中顯示的項(xiàng)目列表作為內(nèi)容,您將通過提供的偵聽器收到所選項(xiàng)目的通知。該列表將在每個(gè)選中項(xiàng)目的文本右側(cè)顯示一個(gè)復(fù)選標(biāo)記。單擊列表中的項(xiàng)目不會(huì)關(guān)閉對話框。單擊按鈕將關(guān)閉對話框。
open AlertDialog.Builder! setNegativeButton(textId: Int, listener: DialogInterface.OnClickListener!)設(shè)置在按下對話框的Negative按鈕時(shí)要調(diào)用的偵聽器。
open AlertDialog.Builder! setNegativeButton(text: CharSequence!, listener: DialogInterface.OnClickListener!)設(shè)置在按下對話框的Negative按鈕時(shí)要調(diào)用的偵聽器。
open AlertDialog.Builder! setNeutralButton(textId: Int, listener: DialogInterface.OnClickListener!)設(shè)置在按下對話框的Neutral按鈕時(shí)要調(diào)用的偵聽器。
open AlertDialog.Builder! setNeutralButton(text: CharSequence!, listener: DialogInterface.OnClickListener!)設(shè)置在按下對話框的Neutral按鈕時(shí)要調(diào)用的偵聽器。
open AlertDialog.Builder! setOnCancelListener(onCancelListener: DialogInterface.OnCancelListener!)設(shè)置在對話框取消時(shí)將調(diào)用的回調(diào)。 即使在可取消的對話框中,對話框也可能由于被取消或提供的選項(xiàng)之一被選擇以外的原因而被關(guān)閉。如果您有興趣偵聽對話框被關(guān)閉的所有情況,而不僅僅是在取消時(shí),請參閱 setOnDismissListener
open AlertDialog.Builder! setOnDismissListener(onDismissListener: DialogInterface.OnDismissListener!)設(shè)置在對話框因任何原因關(guān)閉時(shí)將調(diào)用的回調(diào)。
open AlertDialog.Builder! setOnItemSelectedListener(listener: AdapterView.OnItemSelectedListener!)設(shè)置在選擇列表中的項(xiàng)目時(shí)要調(diào)用的偵聽器。
open AlertDialog.Builder! setOnKeyListener(onKeyListener: DialogInterface.OnKeyListener!)設(shè)置將在將key分派到對話框時(shí)調(diào)用的回調(diào)。
open AlertDialog.Builder! setPositiveButton(textId: Int, listener: DialogInterface.OnClickListener!)設(shè)置在按下對話框的Positive按鈕時(shí)要調(diào)用的偵聽器。
open AlertDialog.Builder! setPositiveButton(text: CharSequence!, listener: DialogInterface.OnClickListener!)設(shè)置在按下對話框的Positive按鈕時(shí)要調(diào)用的偵聽器。
open AlertDialog.Builder! setSingleChoiceItems(itemsId: Int, checkedItem: Int, listener: DialogInterface.OnClickListener!)設(shè)置要在對話框中顯示的項(xiàng)目列表作為內(nèi)容,您將通過提供的偵聽器收到所選項(xiàng)目的通知。這應(yīng)該是一個(gè)數(shù)組類型,即 R.array.foo 該列表將在選中項(xiàng)的文本右側(cè)顯示一個(gè)復(fù)選標(biāo)記。單擊列表中的項(xiàng)目不會(huì)關(guān)閉對話框。單擊按鈕將關(guān)閉對話框。
open AlertDialog.Builder! setSingleChoiceItems(cursor: Cursor!, checkedItem: Int, labelColumn: String!, listener: DialogInterface.OnClickListener!)設(shè)置要在對話框中顯示的項(xiàng)目列表作為內(nèi)容,您將通過提供的偵聽器收到所選項(xiàng)目的通知。該列表將在選中項(xiàng)目的文本右側(cè)顯示一個(gè)復(fù)選標(biāo)記。單擊列表中的項(xiàng)目不會(huì)關(guān)閉對話框。單擊按鈕將關(guān)閉對話框。
open AlertDialog.Builder! setSingleChoiceItems(items: Array<CharSequence!>!, checkedItem: Int, listener: DialogInterface.OnClickListener!)設(shè)置要在對話框中顯示的項(xiàng)目列表作為內(nèi)容,您將通過提供的偵聽器收到所選項(xiàng)目的通知。該列表將在選中項(xiàng)目的文本右側(cè)顯示一個(gè)復(fù)選標(biāo)記。單擊列表中的項(xiàng)目不會(huì)關(guān)閉對話框。單擊按鈕將關(guān)閉對話框。
open AlertDialog.Builder! setSingleChoiceItems(adapter: ListAdapter!, checkedItem: Int, listener: DialogInterface.OnClickListener!)設(shè)置要在對話框中顯示的項(xiàng)目列表作為內(nèi)容,您將通過提供的偵聽器收到所選項(xiàng)目的通知。該列表將在選中項(xiàng)目的文本右側(cè)顯示一個(gè)復(fù)選標(biāo)記。單擊列表中的項(xiàng)目不會(huì)關(guān)閉對話框。單擊按鈕將關(guān)閉對話框。.
open AlertDialog.Builder! setTitle(titleId: Int)使用給定的資源 ID 設(shè)置標(biāo)題。
open AlertDialog.Builder! setTitle(title: CharSequence!)設(shè)置對話框中顯示的標(biāo)題。
open AlertDialog.Builder! setView(layoutResId: Int)將自定義視圖資源設(shè)置為對話框的內(nèi)容。資源將被膨脹,將所有頂級視圖添加到屏幕上。
open AlertDialog.Builder! setView(view: View!)將自定義視圖設(shè)置為警報(bào)對話框的內(nèi)容。 使用前 Holo 主題時(shí),如果提供的視圖是 ListView 的實(shí)例,則將使用淺色背景。 注意:為了確保樣式一致,自定義視圖應(yīng)該使用通過 getContext() 獲得的警報(bào)對話框的主題上下文來膨脹或構(gòu)造。
open AlertDialog! show()使用提供給此構(gòu)建器的參數(shù)創(chuàng)建 AlertDialog 并立即顯示對話框。
//Theme.AppCompat.Dialog主題常用的一般有以下屬性:
<!-- 背景透明 -->  
<item name="android:windowBackground">@android:color/transparent</item>  
<!-- 邊框 -->  
<item name="android:windowFrame">@null</item>  
<!-- 是否浮現(xiàn)在activity之上 -->  
<item name="android:windowIsFloating">true</item>  
<!-- 是否半透明 -->  
<item name="android:windowIsTranslucent">true</item>  
<!-- 是否無標(biāo)題 -->  
<item name="android:windowNoTitle">true</item>  
<!-- Dialog背景樣式 --> 
<item name="android:background">@android:color/transparent</item>  
<!-- 模糊 -->  
<item name="android:backgroundDimEnabled">true</item>  
<!-- 遮罩層 -->  
<item name="android:backgroundDimAmount">0.5</item> 
<!-- Dialog的寬度和屏幕的比例 -->  
<item name="android:windowMinWidthMinor">80%</item>
<!-- Dialog的最小寬度 -->  
<item name="android:minWidth">300dp</item>
<!-- Dialog的最小高度 -->  
<item name="android:minHeight">200dp</item>

AlertDialog 的使用

1.提示對話框

val builder = AlertDialog.Builder(this)
builder.setTitle("Title")
builder.setMessage("Message")
//設(shè)置Neutral按鈕(最左邊的按鈕)
builder.setNeutralButton("Neutral") { _, _ ->
    Toast.makeText(
        this@MainActivity,
        "點(diǎn)擊了Neutral",
        Toast.LENGTH_SHORT
    ).show()
}
//設(shè)置Positive按鈕
builder.setPositiveButton("Positive") { _, _ ->
    Toast.makeText(
        this@MainActivity,
        "點(diǎn)擊了Positive",
        Toast.LENGTH_SHORT
    ).show()
}
//設(shè)置Negative按鈕
builder.setNegativeButton("Negative") { _, _ ->
    Toast.makeText(
        this@MainActivity,
        "點(diǎn)擊了Negative",
        Toast.LENGTH_SHORT
    ).show()
}
//設(shè)置在對話框因任何原因關(guān)閉時(shí)將調(diào)用的回調(diào)。
builder.setOnDismissListener {
    Toast.makeText(
        this@MainActivity,
        "對話框Dismiss了",
        Toast.LENGTH_SHORT
    ).show()
}
//設(shè)置在對話框取消時(shí)將調(diào)用的回調(diào)
//        builder.setOnCancelListener {
//            Toast.makeText(
//                this@MainActivity,
//                "對話框Cancel了",
//                Toast.LENGTH_SHORT
//            ).show()
//        }
//點(diǎn)擊對話框以外的區(qū)域是否讓對話框消失,默認(rèn)為true;
builder.setCancelable(false)

//設(shè)置對話框的title左邊的圖標(biāo);
builder.setIcon(resources.getDrawable(R.drawable.ic_launcher_background,null))

val dialog = builder.create()
//這個(gè)是Dialog中的方法,builder中沒有這個(gè)方法
dialog.setOnShowListener {
    Toast.makeText(
        this@MainActivity,
        "對話框Show了",
        Toast.LENGTH_SHORT
    ).show()
}
dialog.show()
截屏2021-09-01 上午9.44.28.png
  1. 列表對話框

    val builder = AlertDialog.Builder(this)
    builder.setCancelable(false)
    builder.setTitle("這是ListDialog")
    builder.setIcon(R.mipmap.ic_launcher)
    builder.setNegativeButton("取消") { _, _ ->
      Toast.makeText(
          this@MainActivity,
          "點(diǎn)擊取消",
          Toast.LENGTH_SHORT
      ).show()
    }
    
    builder.setPositiveButton("確定") { _, _ ->
      Toast.makeText(
          this@MainActivity,
          "點(diǎn)擊確定",
          Toast.LENGTH_SHORT
      ).show()
    }
    val list = arrayOf("語文", "數(shù)學(xué)", "英語", "化學(xué)", "生物", "物理", "體育")
    //設(shè)置列表
    builder.setItems(list) { p0, p1 ->
      Toast.makeText(
          this@MainActivity,
          "點(diǎn)擊了第${p1}行",
          Toast.LENGTH_SHORT
      ).show()
    }
    builder.create().show()
    
截屏2021-09-01 上午10.12.04.png
  1. 單選對話框

    val builder = AlertDialog.Builder(this)
    builder.setCancelable(false)
    builder.setTitle("這是ListDialog")
    builder.setIcon(R.mipmap.ic_launcher)
    builder.setNegativeButton("取消") { _, _ ->
        Toast.makeText(
            this@MainActivity,
            "點(diǎn)擊取消",
            Toast.LENGTH_SHORT
        ).show()
    }
    
    builder.setPositiveButton("確定") { _, _ ->
        Toast.makeText(
            this@MainActivity,
            "點(diǎn)擊確定",
            Toast.LENGTH_SHORT
        ).show()
    }
    val list = arrayOf("語文", "數(shù)學(xué)", "英語", "化學(xué)", "生物", "物理", "體育")
    //設(shè)置列表
    
    builder.setSingleChoiceItems(list,3) { p0, p1 ->
        Toast.makeText(
            this@MainActivity,
            "點(diǎn)擊了第${p1}行",
            Toast.LENGTH_SHORT
        ).show()
    }
    builder.create().show()
    
截屏2021-09-01 上午10.15.53.png
  1. 多選對話框

    val builder = AlertDialog.Builder(this)
            builder.setCancelable(false)
            builder.setTitle("這是ListDialog")
            builder.setIcon(R.mipmap.ic_launcher)
            builder.setNegativeButton("取消") { _, _ ->
                Toast.makeText(
                    this@MainActivity,
                    "點(diǎn)擊取消",
                    Toast.LENGTH_SHORT
                ).show()
            }
    
            builder.setPositiveButton("確定") { _, _ ->
                Toast.makeText(
                    this@MainActivity,
                    "點(diǎn)擊確定",
                    Toast.LENGTH_SHORT
                ).show()
            }
            val list = arrayOf("語文", "數(shù)學(xué)", "英語", "化學(xué)", "生物", "物理", "體育")
            var checked = booleanArrayOf(false,false,true,false,false,true,false)
            //設(shè)置列表
            builder.setMultiChoiceItems(list,checked
            ) { _, p1, p2 ->
                Toast.makeText(
                    this@MainActivity,
                    "點(diǎn)擊了第${p1}行 $p2",
                    Toast.LENGTH_SHORT
                ).show()
            }
            builder.create().show()
    
截屏2021-09-01 上午10.29.13.png
  1. 自定義彈窗

     val builder = AlertDialog.Builder(this, R.style.CustomDialog)
         builder.setCancelable(true)
         val view = layoutInflater.inflate(R.layout.custom_layout, null, false)
         val binding = CustomLayoutBinding.bind(view)
    
         builder.setView(view)
         val dialog =  builder.create()
         dialog.show()
    
         binding.cancelButton.setOnClickListener {
             Toast.makeText(
                 this@MainActivity,
                 "點(diǎn)擊了取消",
                 Toast.LENGTH_SHORT
             ).show()
             dialog.dismiss()
         }
         binding.okButton.setOnClickListener {
             Toast.makeText(
                 this@MainActivity,
                 "點(diǎn)擊了確認(rèn)",
                 Toast.LENGTH_SHORT
             ).show()
         }
    

    custom_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
        <Button
            android:id="@+id/cancel_button"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:layout_marginLeft="40dp"
            android:backgroundTint="#f00"
            android:text="取消"
            android:textColor="#0f0"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    
        <Button
            android:id="@+id/ok_button"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:layout_marginRight="40dp"
            android:backgroundTint="#0f0"
            android:text="確定"
            android:textColor="#f00"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />
    
        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:layout_marginTop="60dp"
            android:gravity="center"
            android:text="Message"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@id/title"
            app:layout_constraintBottom_toTopOf="@id/cancel_button"
            />
    
        <TextView
            android:id="@+id/title"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:gravity="center"
            android:text="標(biāo)題"
            app:layout_constraintBottom_toTopOf="@id/message"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
    截屏2021-09-01 下午12.09.12.png

    如何修改Dialog的寬度和高度

    直接在layout.xml修改layout_width和layout_height是無法修改Dialog的寬和高的需要自定義一個(gè)theme,使用這個(gè)theme創(chuàng)建AlertDialog.

      <style name="CustomDialog" parent="Theme.AppCompat.Dialog">
            <!-- dialog  寬度與屏幕比例-->
            <!--        <item name="android:windowMinWidthMinor">80%</item>-->
            <!--設(shè)置最小寬度-->
            <item name="android:minWidth">300dp</item>
            <!--設(shè)置最小高度-->
            <item name="android:minHeight">200dp</item>
        </style>
    

    通過代碼修改AlertDialog的大小

    //這段代碼必須放在show()方法之后,否則沒有作用
    dialog.window?.setLayout(100,200)
    

    如何修改AlertDialog的位置

    //這段代碼必須放在show()方法之前,否則沒有作用 當(dāng)x,y都為0則居中顯示
    val para = dialog.window?.attributes
    para?.x = 100 
    para?.y = 100
    
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,501評論 6 544
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,673評論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,610評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,939評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,668評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,004評論 1 329
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,001評論 3 449
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,173評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,705評論 1 336
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,426評論 3 359
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,656評論 1 374
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,139評論 5 364
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,833評論 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,247評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,580評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,371評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,621評論 2 380

推薦閱讀更多精彩內(nèi)容