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

时间:2013-01-10 11:46:38

标签: android android-webview center

我在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 个答案:

答案 0 :(得分: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);

享受!

答案 1 :(得分: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));

感谢。

答案 2 :(得分:0)

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

ansCellLayout.setGravity ( Gravity.CENTER );

答案 3 :(得分:-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>