我在 xml 中有一个 LinearLayout,现在我将 webview 添加到该 LinearLayout 但 webview 显示在顶部,它没有在中心对齐。
这就是它现在的显示方式。
webview content
--------------------
webview content
--------------------
webview content
--------------------
代码
WebView answerHtml = new WebView(this);
answerHtml.setId(i);
answerHtml.setOnTouchListener(AnswerListener);
answerHtml.setBackgroundColor(0);
answerHtml.setBackgroundResource(R.drawable.ans_back);
answerHtml.getSettings().setJavaScriptEnabled(true);
answerHtml.loadDataWithBaseURL(null, a.getText(), "text/html", "utf-8", null);
ansCellLayout.addView(answerHtml,new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,80));
ansCellLayout is a Linearlayout defined in xml.
解决了
answerHtml.loadDataWithBaseURL(null,"<html><head><style type='text/css'>html,body {margin: 0;padding: 0;width: 100%;height: 100%;}html {display: table;}body {display: table-cell;vertical-align: middle;text-align: center;}</style></head><body><p>"+a.getText()+"</p></body></html>", "text/html", "utf-8", null);
现在它垂直居中于布局。