2010-08-31 28 views
2

也許我錯過了一些明顯的東西,但我很難爲AlertDialog的主體設置自定義視圖。下面是我在做來設置自定義視圖什麼:AlertDialog setContentView接管屏幕

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.setContentView(View.inflate(getContext(), R.layout.dialog_body, null)); 
} 

而不是設置AlertDialog的正文的內容,有觀點被放置在整個屏幕。我如何獲取視圖只是爲了替換AlertDialog消息正文?

+0

其onCreate函數是那個,你的活動? – 2010-08-31 00:55:41

+0

擴展AlertDialog的類。但是我使用AlertDialog.Builder。 – Gumgo 2010-09-03 06:29:40

+0

我很想有一個AlertDialog的onCreate解決方案。 – 2013-09-20 15:47:58

回答

2

您正在通過調用setContentView來設置活動的視圖,這就是它佔據整個屏幕的原因。你也正在做我認爲是活動的onCreate方法,你需要在onCreateDialog方法中做到這一點。

繼承人鏈接到docs和一個例子。

public Dialog onCreateDialog(int id) { 

      Dialog dialog = null; 
      AlertDialog.Builder builder = new AlertDialog.Builder(app); 
      AlertDialog alert = null; 

      builder.setTitle("A title") 
        .setCancelable(true) 
        .setView(myView); 
       alert = builder.create(); 
       return alert; 

} 
+0

它實際上是在對話框的onCreate中,但我會給它一個鏡頭。謝謝! – Gumgo 2010-09-01 21:14:11