自定義AlertDialog樣式,根據屏幕大小來顯示


介紹一些關於AlertDialog的基本知識:

    一、AlertDialog簡介:AlertDialog的構造方法被聲明為protected,所以不能直接使用new關鍵字來創建 AlertDialog類的對象實例。要想創建AlertDialog對話框,需要使用Builder類,該類是AlertDialog類中定義的一個內 嵌類。因此必須創建AlertDialog.Builder類的對象實例,然后再調用show()來顯示對話框。


    二、使用AlertDialog創建對話框的種類:


        1. 最多帶3個按鈕的對話框:setPositiveButton(...)--確認、setNegativeButton(...)--取消、setNeutralButton(...)--忽略


        2.簡單列表對話框:通過AlertDialog.Builder類的setItems(...)方法可以創建簡單的列表對話框。其實,這種類型的對話框相當於將ListView組件放在對話框上,然后再在ListView中添加若干簡單的文本。


        3.單選列表對話框:通過AlertDialog.Builder類的setSingleChoiceItems(...)來創建。目前支持4種數據源(數組資源、數據集、字符串數組、ListAdapter)


        4.多選列表對話框:通過AlertDialog.Builder類的setMultiChoiceItems(...)創建。目前支持3種數據源(數組資源、數據集、字符串數組)


        5.水平進度或圓形對話框(默認是:圓形):該類型的對話框是通過ProgressDialog來實現,該類是AlertDialog的子類,它不需要用create()方法來返回實例對象,只需要new即可。


           ProgressDialog.STYLE_HORIZONTAL //水平進度樣式


           ProgressDialog.STYLE_SPINNER    //圓形樣式


        6.自定義對話框:直接使用XML布局文件或以編寫JAVA代碼方式來創建視圖,並將這些視圖對象添加到對話框中去。


        7.使用Activity托管對話框:Activity類中也提供了創建對話框的方式,有個onCreateDialog(int id)的方法,其返回類型是Dialog,通過是當調用Activity類的showDialog(int id)方法時,系統會調用該方法來返回一個Dialog對象。showDialog和onCreateDialog都有一個int類型的id參數,該參數 值將傳遞給onCreateDialog方法。因此,我們可以利用不同的id創建多個對話框。


         ***注意***:對於表示某一個對話框的ID,系統只在第1次調用showDialog方法時調用onCreateDialog方法。在第1次創建 Dialog對象時系統會將該對象保存在Activity的緩存里,相當於一個Map對象,對話框的ID作為Map的Key,而Dialog對象作為 Map的Value。下次再調用時,會先根據這個ID從Map中獲得第1次創建的Dialog對象。除非該ID已經被刪除。


        8.懸浮對話框和觸摸任何位置都可以關閉的對話框:


        1).懸浮對話框:android:theme="@android:style/Theme.Dialog";對於該類型的對話框,觸摸屏幕任何位置都會觸發Activity的OnTouchEvent事件。


         2).觸摸任何位置都可以關閉的對話框:首先必須要繼承AlertDialog類,並重寫OnTouchEvent事件。


第一種:


Java代碼

  1. /**
  2. * 自定義AlertDialog
  3. *
  4. * @author chenjianli 2011-05-10
  5. */
  6. public void alert(){
  7.         WindowManager manager = getWindowManager();
  8.         Display display = manager.getDefaultDisplay();
  9.         int width = display.getWidth();
  10.         int height = display.getHeight();

  11.         LayoutInflater inflater = getLayoutInflater();
  12.         View view = inflater.inflate(R.layout.alert, null);

  13.         TextView text = (TextView)view.findViewById(R.id.text);
  14.         text.setText("自定義AlertDialog");

  15.         AlertDialog alert = new AlertDialog.Builder(this).create();
  16.         alert.show();

  17.         alert.getWindow().setLayout(width/2, height/4);
  18.         alert.setTitle("測試");
  19.         alert.getWindow().setContentView(R.layout.alert);
  20. }
復制代碼

第二種:
Java代碼

  1. /**
  2. * 自定義AlertDialog
  3. *
  4. * @author chenjianli 2011-05-10
  5. */

  6. AlertDialog zidongbofangDialog = new AlertDialog.Builder(ManHuaActivity.this).create();
  7. zidongbofangDialog.show();
  8. zidongbofangDialog.getWindow().setGravity(Gravity.CENTER);
  9. zidongbofangDialog.getWindow().setLayout(
  10. android.view.WindowManager.LayoutParams.FILL_PARENT,
  11. android.view.WindowManager.LayoutParams.WRAP_CONTENT);
  12. zidongbofangDialog.getWindow().setContentView(R.layout.manhua_dialog_zidongbofang);
復制代碼

第三種:
/**
* 自定義AlertDialog
*
* @author chenjianli 2011-05-10
*/
如果我們setView(),中的View是帶EditText的,此時,我們必須在show()之前加上這么一句話,才可以在點擊EditText時彈出鍵盤,否則將很杯具!鍵盤是彈不出來的。
Java代碼

  1. AlertDialog tiaozhuanDialog= new AlertDialog.Builder(ManHuaActivity.this).create();
  2. tiaozhuanDialog.setView(getLayoutInflater().inflate(R.layout.manhua_dialog_tiaozhuan, null));
  3. tiaozhuanDialog.show();
  4. tiaozhuanDialog.getWindow().setGravity(Gravity.CENTER);
  5. tiaozhuanDialog.getWindow().setLayout(
  6. android.view.WindowManager.LayoutParams.FILL_PARENT,
  7. android.view.WindowManager.LayoutParams.WRAP_CONTENT);
  8. tiaozhuanDialog.getWindow().setContentView(getLayoutInflater().inflate(R.layout.manhua_dialog_tiaozhuan, null));
復制代碼

這里還有一個地方需要注意一下,如果我們在show這個AlertDialog之前,需要設置該AlertDialog顯示的View中的EditText的內容,則我們應該這么去findViewById():
Java代碼

  1. EditText editText = (EditText)tiaozhuanDialog.findViewById(R.id.myEditText);
  2. editText.setText("Who are you ? I am android Developer ");
復制代碼

否 則會報ERROR/AndroidRuntime(1032): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.錯誤!!

 

 

 

 1 1)更改AlertDialog窗口大小的方法:
2 AlertDialog dialog = new AlertDialog.Builder(this).create();
3 dialog.show();
4 WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
5 params.width = 200;
6 params.height = 200 ;
7 dialog.getWindow().setAttributes(params);
8
9 2)去除邊框
10 AlertDialog.setView(view,0,0,0,0);


[java] view plain copy
  1. AlertDialog dialog = builder.setTitle("消息列表")  
  2.                     .setView(layout)  
  3.                     .create();  
  4. dialog.show();  
  5. //設置窗口的大小  
  6. dialog.getWindow().setLayout(300200);  

dialog.show();一定要放在dialog.getWindow().setLayout(300, 200);的前面,否則不起作用。

網上有一種方法是
[java] view plain copy
  1. WindowManager.LayoutParams params = dialog.getWindow().getAttributes();  
  2. params.width = 300;  
  3. params.height = 200;  
  4. dialog.getWindow().setAttributes(params);  
但是dialog.getWindow().setLayout(300, 200);實際上封裝了這個方法,setLayout()的源代碼如下:
[java] view plain copy
  1. final WindowManager.LayoutParams attrs = getAttributes();  
  2. attrs.width = width;  
  3. attrs.height = height;  
  4. if (mCallback != null) {  
  5.     mCallback.onWindowAttributesChanged(attrs);  
  6. }  

所以這兩個方法的作用本質上是一樣的,都是為AlertDialog設置大小


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM