自定義彈窗

 1、僅更改彈窗的顯示內容

View view = View.inflate(this, R.layout.**, null);

new AlertDialog.Builder(this).setView(view);

2、更改彈窗的背景

如果使用AlertDialog的setView,彈窗的周邊會有黑邊出現,這時候則需要這麼做

  1. Dialog dialog = new Dialog(this, R.style.dialog_style); 
  2. View view = View.inflate(this, R.layout.**, null); 
  3. dialog.setContentView(view); 
  4. dialog.show(); 
  5. dialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); //自適應大小

dialog_style:

  1. <style name = "dialog_style" parent="@android:style/Theme.Dialog"> 
  2.      <item name = "android:windowBackground">@drawable/dialog_bg</item> 
  3.       <item name = "android:windowNoTitle">true</item> 
  4. </style> 

dialog_bg:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" > 
  3.     <solid android:color="#00000000"/> 
  4. </shape> 

dialog_bg 用於去掉黑色邊,View中設置好需要的背景.

3、如果使用AlertDialog

AlertDialog dialog = AlertDialog.Builder(this).create();

dialog.show();

Window = dialog.getWindow();

view.setContentView(R.layout.**);

使用這種方法可以達到背景更換的效果,但是如果添加了EditText會出現,EditText不能彈出輸入法的問題?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章