我需要呈现一个包含 html 内容的对话框,为此我使用 AlertDialog.Builder 创建一个基于包含 WebView 的自定义视图的对话框。
只要 html 字符串不包含 style 标签,WebView 就会在对话框中正确呈现,在这种情况下,由于我未知的原因,您只会看到一个空的 WebView。
showDialog:
fun showDialog(message: String) {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val dialog = inflater.inflate(R.layout.customdialog_layout_knowmylevel, null)
val webView = dialog.findViewById<WebView>(R.id.webview_dialog_text)
(webView.parent as LinearLayout).removeAllViews()
val settings = webView.settings
settings.javaScriptEnabled = true
//webView.loadData(message, "text/html; charset=UTF-8",null) => OPTION 1
webView.loadDataWithBaseURL("", message,"text/html","utf-8","") //=> OPTION 2
//webView.measure(100, 100) => TESTING (NOT WORKING)
//settings.useWideViewPort = true => TESTING (NOT WORKING)
//settings.loadWithOverviewMode = true => TESTING (NOT WORKING)
val alertDialog: Dialog = AlertDialog.Builder(tm.context)
.setTitle("My Report")
.setView(webView)
.setPositiveButton("Close"
) { _, _ -> dismiss() }.create()
alertDialog.show()
}
customdialog_layout_knowmylevel:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dialog_container"
android:orientation="vertical"
tools:ignore="SelectableText" >
<TextView
android:id="@+id/textview_dialog_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|start"
android:padding="10sp"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<WebView
android:id="@+id/webview_dialog_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:alwaysDrawnWithCache="false" />
<LinearLayout
android:id="@+id/tablelayout_dialog_buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/button_dialog_yes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_weight="1" />
<Button
android:id="@+id/button_dialog_no"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
Message for the dialog (no <style>):
val message = "<!DOCTYPE html>" +
"<html>" +
"<head>" +
"</head>" +
"<body>" +
" <h1>Math Test Evaluation Report</h1>" +
" " +
" <h2>1. Summary of Scores</h2>" +
" <table>" +
" <tr>" +
" <th>Test</th>" +
" <th>Score</th>" +
" </tr>" +
" <tr>" +
" <td>SAT Math Level 1 Test 1</td>" +
" <td>Various Scores</td>" +
" </tr>" +
" <tr>" +
" <td>SAT Math Level 1 Test 2</td>" +
" <td>40, 0, 0, 0</td>" +
" </tr>" +
" <tr>" +
" <td>SAT Math Level 2 Test 1</td>" +
" <td>0, 20, 0, 0, 0, 0</td>" +
" </tr>" +
" <tr>" +
" <td>SAT Math Level 3 Test 1</td>" +
" <td>0, 0</td>" +
" </tr>" +
" </table>" +
" " +
" <h2>2. Statistical Analysis</h2>" +
" <p>[Statistical Analysis Here]</p>" +
" " +
" <h2>3. Score Analysis</h2>" +
" <p>[Score Analysis Here]</p>" +
" " +
" <h2>4. Interpretation of Results</h2>" +
" <p>[Interpretation of Results Here]</p>" +
" " +
" <h2>5. Evaluation of Performance</h2>" +
" <p>[Evaluation of Performance Here]</p>" +
" " +
" <h2>6. Strengths</h2>" +
" <p>[Strengths Here]</p>" +
" " +
" <h2>7. Areas for Improvement</h2>" +
" <p>[Areas for Improvement Here]</p>" +
" " +
" <h2>8. Recommendations</h2>" +
" <p>[Recommendations Here]</p>" +
" " +
" <h2>9. Conclusion</h2>" +
" <p>[Conclusion Here]</p>" +
" " +
" <div class=\"chart-container\">" +
" <!-- Include Charts Here -->" +
" <h2>Test Scores Chart</h2>" +
" <img class=\"center-fit\" src=\"https://quickchart.io/chart?c={type:'bar',data:{labels:['Test 1', 'Test 2', 'Test 3'],datasets:[{label:'Scores',data:[75, 30, 10]}]}}\">" +
" </div>" +
"</body>" +
"</html>"
Result:
Message for the dialog (with <style>):
val response = "<!DOCTYPE html>" +
"<html>" +
"<head>" +
" <style>\n" +
" * {\n" +
" margin: 0;\n" +
" padding: 0;\n" +
" }\n" +
" .imgbox {\n" +
" display: grid;\n" +
" height: 100%;\n" +
" }\n" +
" .center-fit {\n" +
" max-width: 100%;\n" +
" max-height: 100vh;\n" +
" margin: auto;\n" +
" }\n" +
" </style>"
"</head>" +
"<body>" +
" <h1>Math Test Evaluation Report</h1>" +
" " +
" <h2>1. Summary of Scores</h2>" +
" <table>" +
" <tr>" +
" <th>Test</th>" +
" <th>Score</th>" +
" </tr>" +
" <tr>" +
" <td>SAT Math Level 1 Test 1</td>" +
" <td>Various Scores</td>" +
" </tr>" +
" <tr>" +
" <td>SAT Math Level 1 Test 2</td>" +
" <td>40, 0, 0, 0</td>" +
" </tr>" +
" <tr>" +
" <td>SAT Math Level 2 Test 1</td>" +
" <td>0, 20, 0, 0, 0, 0</td>" +
" </tr>" +
" <tr>" +
" <td>SAT Math Level 3 Test 1</td>" +
" <td>0, 0</td>" +
" </tr>" +
" </table>" +
" " +
" <h2>2. Statistical Analysis</h2>" +
" <p>[Statistical Analysis Here]</p>" +
" " +
" <h2>3. Score Analysis</h2>" +
" <p>[Score Analysis Here]</p>" +
" " +
" <h2>4. Interpretation of Results</h2>" +
" <p>[Interpretation of Results Here]</p>" +
" " +
" <h2>5. Evaluation of Performance</h2>" +
" <p>[Evaluation of Performance Here]</p>" +
" " +
" <h2>6. Strengths</h2>" +
" <p>[Strengths Here]</p>" +
" " +
" <h2>7. Areas for Improvement</h2>" +
" <p>[Areas for Improvement Here]</p>" +
" " +
" <h2>8. Recommendations</h2>" +
" <p>[Recommendations Here]</p>" +
" " +
" <h2>9. Conclusion</h2>" +
" <p>[Conclusion Here]</p>" +
" " +
" <div class=\"chart-container\">" +
" <!-- Include Charts Here -->" +
" <h2>Test Scores Chart</h2>" +
" <img class=\"center-fit\" src=\"https://quickchart.io/chart?c={type:'bar',data:{labels:['Test 1', 'Test 2', 'Test 3'],datasets:[{label:'Scores',data:[75, 30, 10]}]}}\">" +
" </div>" +
"</body>" +
"</html>"
Result:
我已经检查过android webview显示空白页面
和https://github.com/beeware/toga/issues/2242
无济于事,因为如果我向 html 字符串添加 style 标签,我的 WebView 会不断呈现空白页面。
接下来我可以尝试什么?
添加加号图标后
</style>
标签完成
val response = "<!DOCTYPE html>" +
"<html>" +
"<head>" +
" <style>\n" +
" * {\n" +
" margin: 0;\n" +
" padding: 0;\n" +
" }\n" +
" .imgbox {\n" +
" display: grid;\n" +
" height: 100%;\n" +
" }\n" +
" .center-fit {\n" +
" max-width: 100%;\n" +
" max-height: 100vh;\n" +
" margin: auto;\n" +
" }\n" +
" </style>" + <<-- add Plus after style Tag finish
"</head>" +
"<body>" +
" <h1>Math Test Evaluation Report</h1>" +
" " +
" <h2>1. Summary of Scores</h2>" +
" <table>" +
" <tr>" +
" <th>Test</th>" +
" <th>Score</th>" +
" </tr>" +
" <tr>" +
" <td>SAT Math Level 1 Test 1</td>" +
" <td>Various Scores</td>" +
" </tr>" +
" <tr>" +
" <td>SAT Math Level 1 Test 2</td>" +
" <td>40, 0, 0, 0</td>" +
" </tr>" +
" <tr>" +
" <td>SAT Math Level 2 Test 1</td>" +
" <td>0, 20, 0, 0, 0, 0</td>" +
" </tr>" +
" <tr>" +
" <td>SAT Math Level 3 Test 1</td>" +
" <td>0, 0</td>" +
" </tr>" +
" </table>" +
" " +
" <h2>2. Statistical Analysis</h2>" +
" <p>[Statistical Analysis Here]</p>" +
" " +
" <h2>3. Score Analysis</h2>" +
" <p>[Score Analysis Here]</p>" +
" " +
" <h2>4. Interpretation of Results</h2>" +
" <p>[Interpretation of Results Here]</p>" +
" " +
" <h2>5. Evaluation of Performance</h2>" +
" <p>[Evaluation of Performance Here]</p>" +
" " +
" <h2>6. Strengths</h2>" +
" <p>[Strengths Here]</p>" +
" " +
" <h2>7. Areas for Improvement</h2>" +
" <p>[Areas for Improvement Here]</p>" +
" " +
" <h2>8. Recommendations</h2>" +
" <p>[Recommendations Here]</p>" +
" " +
" <h2>9. Conclusion</h2>" +
" <p>[Conclusion Here]</p>" +
" " +
" <div class=\"chart-container\">" +
" <!-- Include Charts Here -->" +
" <h2>Test Scores Chart</h2>" +
" <img class=\"center-fit\" src=\"https://quickchart.io/chart?c={type:'bar',data:{labels:['Test 1', 'Test 2', 'Test 3'],datasets:[{label:'Scores',data:[75, 30, 10]}]}}\">" +
" </div>" +
"</body>" +
"</html>"