Android 以编程方式将webview与LinearLayout的中心对齐

Android 以编程方式将webview与LinearLayout的中心对齐,android,android-webview,center,Android,Android Webview,Center,我在xml中有一个LinearLayout,现在我将webview添加到该LinearLayout中,但webview显示在顶部,而不是在中间对齐 这就是它现在的显示方式 webview content -------------------- webview content -------------------- webview content -------------------- 代码 已解决 answerHtml.loadDataWithBaseURL(null,"<html

我在xml中有一个LinearLayout,现在我将webview添加到该LinearLayout中,但webview显示在顶部,而不是在中间对齐

这就是它现在的显示方式

webview content
--------------------
webview content
--------------------
webview content
--------------------
代码

已解决

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);
answerHtml.loadDataWithBaseURL(null,“html,正文{margin:0;padding:0;宽度:100%;高度:100%;}html{display:table;}正文{display:table单元格;垂直对齐:中间;文本对齐:中间;}“+a.getText()+”

”,“text/html”,“utf-8”,null);

现在它垂直居中于布局。

尝试如下设置线条布局:

编辑1:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true">
</LinearLayout>

</RelativeLayout>

谢谢。

尝试在活动中使用以下代码:

ansCellLayout.setGravity ( Gravity.CENTER );
试试这个(这是Pratik帖子的编辑):


使用此示例并对其进行自定义:

            String desiredContent = "my test html";
            String strBody = "<html>"
                + "<head>"
                + "     <style type=\"text/css\">"
                + "     @font-face "
                + "         {   font-family: Tahoma;"
                + "             src:url(\"~/android_asset/fonts/MyriadPro-Regular.otf\") "
                + "         }"
                + "     #text"
                + "         {   font-family: Tahoma;"
                + "             font-size:14;"
                + "             text-align: center;"
                + "         }"
                + "     </style>"
                + ""
                + "</head>"
                + "<body dir=\"rtl\" id=\"text\">"
                + desiredContent 
                + " </body></html>  ";

        myWebView.loadDataWithBaseURL(null, strBody, "text/html", "utf-8",
                null);
String desiredContent=“我的测试html”;
字符串strBody=“”
+ ""
+ "     "
+“@font-face”
+“{字体系列:Tahoma;”
+“src:url(\“~/android\u asset/fonts/myriadro Regular.otf\”)
+ "         }"
+“#文本”
+“{字体系列:Tahoma;”
+“字号:14;”
+“文本对齐:居中;”
+ "         }"
+ "     "
+ ""
+ ""
+ ""
+期望内容
+ "   ";
myWebView.loadDataWithBaseURL(空,strBody,“text/html”,“utf-8”,
无效);

享受吧

@Goofy查看我的编辑我已经发布了完整的代码,对我来说很好。我会尝试一下mintue@Goofy试试“编辑1”,我已经发布了这个对我也有用。@Goofy这对你有帮助吗?
ansCellLayout.setGravity ( Gravity.CENTER );
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:centerHorizontal="true">
    </LinearLayout>

</RelativeLayout>
            String desiredContent = "my test html";
            String strBody = "<html>"
                + "<head>"
                + "     <style type=\"text/css\">"
                + "     @font-face "
                + "         {   font-family: Tahoma;"
                + "             src:url(\"~/android_asset/fonts/MyriadPro-Regular.otf\") "
                + "         }"
                + "     #text"
                + "         {   font-family: Tahoma;"
                + "             font-size:14;"
                + "             text-align: center;"
                + "         }"
                + "     </style>"
                + ""
                + "</head>"
                + "<body dir=\"rtl\" id=\"text\">"
                + desiredContent 
                + " </body></html>  ";

        myWebView.loadDataWithBaseURL(null, strBody, "text/html", "utf-8",
                null);