2016-10-13 25 views
0

我需要在Android AlertDialog中看到標準xml文件的內容。如何在AlertDialog中放置一個標準的XML頁面 - Android

讓我們考慮一個標準的xml頁面,它可以通過任何互聯網瀏覽器打開,包含一些格式化文本,含有一些子彈點格式,一些白色的行等等(請參見下面的示例)。

我想在AlertDialog中看到標準xml頁面的內容。

通常我由代碼打開AlertDialog:

AlertDialog.Builder myAlertDialogBu​​ilder =新 AlertDialog.Builder(本); myAlertDialogBu​​ilder.setTitle(myTitle); myAlertDialogBu​​ilder.setMessage(myMessage); myAlertDialogBu​​ilder.setPositiveButton(myPositiveButton), dialogTipClickListener); myAlertDialogBu​​ilder.show();

我的目標是有,而不是字符串「myMessage」,XML文件的內容,以XML文件的格式,這意味着格式化例如相同的子彈點,同樣的白色行等...(見下面的例子)。

只是要清楚,我要強調的是標準的XML文件不是一個Android XML佈局,但像在以下幾點:

<!DOCTYPE html> 
<html> 

<head> 
<meta title="Manuale d'uso"> 
<meta charset="ANSI"> 
</head> 

<body> 

<a name="cap4"><h2><font color="blue">Utilizzo</font></h2></a> 

<p> 
    L’utilizzo dell'applicazione è molto intuitivo. 
</p> 

<p> 
    Indicazioni di carattere generale sui colori ....: 
    <ul style="list-style-type:circle"> 
     <li> Le icone in azzurro ..... </li> 
     <li> Le icone in colore grigio .... </li> 
     <li> Le icone in colore verde ..... </li> 
     <li> Le scritte in colore blu ..... </li> 
    </ul> 
</p> 

</body> 
</html> 

預先感謝您

+0

使用webview? – Raghunandan

回答

3

試試這個代碼

AlertDialog.Builder alert = new AlertDialog.Builder(this); 
    alert.setTitle("Title here"); 

    WebView wv = new WebView(this); 
    wv.loadData(<YOURHTML_STING>, "text/html; charset=UTF-8", null); 


    alert.setView(wv); 
    alert.setNegativeButton("Close", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int id) { 
      dialog.dismiss(); 
     } 
    }); 
    alert.show(); 
+0

嗨,謝謝。只是一個問題。 loadData需要一個字符串(),而我有一個inputStream(我的資產中有我的xml文件,並且我可以獲取它的InputStream.Ho可以傳遞一個InputStream到loadData嗎? – Fausto70

+0

已解決!剛使用:'vw。 loadUrl(「file:/// android_asset /」+ nomefile)''而不是'wv.loadData(,「text/html; charset = UTF-8」,null);' – Fausto70

3

您需要創建自定義警報窗口並在警報窗口中使用Web視圖並將您的xml文件加載到該webview。

另一種方法是,你可以創建HTML身體字符串變量和這個字符串變量設置爲您myAlertDialogBu​​ilder.setMessage(StringVariable),如: -

String xmlContentStr = Html.fromHtml("<p>L’utilizzo dell'applicazione è molto intuitivo.</p>"); 

我認爲它的幫助你。

相關問題