2

我在 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);

现在它垂直居中于布局。

4

4 回答 4

1

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

            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);

请享用!

于 2013-08-18T11:03:09.743 回答
0

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

ansCellLayout.setGravity ( Gravity.CENTER );
于 2013-01-10T12:16:53.150 回答
0

尝试像这样设置您的 LineraLayout:

编辑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>

从活动:

        LinearLayout ansCellLayout = (LinearLayout) findViewById(R.id.linearLayout);

        WebView answerHtml = new WebView(this);
        answerHtml.setId(1);
       // answerHtml.setOnTouchListener(AnswerListener);
        answerHtml.setBackgroundColor(0);
        answerHtml.setBackgroundResource(R.drawable.ic_launcher);
        answerHtml.getSettings().setJavaScriptEnabled(true);
        answerHtml.loadDataWithBaseURL(null, "test test", "text/html", "utf-8", null);
        ansCellLayout.addView(answerHtml,new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,80));

谢谢。

于 2013-01-10T11:59:17.330 回答
-1

试试这个(这是对 Pratik 帖子的编辑):

<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>
于 2013-01-10T12:21:52.757 回答