我在xml中有一個LinearLayout,現在我將webview添加到該LinearLayout,但是webview顯示在頂部而不在中間對齊。編程將webview對齊到LinearLayout的中心
這是它現在如何顯示。
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);
現在它的垂直居中的佈局。
沒有它沒有工作我試過 – Goofy
@Goofy看到我的編輯我發佈了完整的代碼,這對我很好。 –
我會嘗試只是一分鐘 – Goofy