如何在垂直中心显示webview数据

时间:2015-04-16 09:50:39

标签: android android-webview mathjax

我希望以下公式在布局中垂直显示中心。它水平居中。但它显示在顶部,而不是中心。

这就是我想要实现的目标 enter image description here

但是,它是如何显示在顶部的: enter image description here

这是我的XML代码:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/title"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="#EFEFEF"
   android:paddingBottom="0dp"
   android:paddingLeft="0dp"
   android:paddingRight="0dp"
   android:paddingTop="0dp" >

<LinearLayout
    android:id="@+id/topLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="#ffd0e3e5"
    android:gravity="center"
    android:paddingBottom="6dp"
    android:paddingTop="5dp" >

    <TextView
        android:id="@+id/textTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="#ff33203d"
        android:textSize="18sp" />
</LinearLayout>

<WebView
    android:id="@+id/txtQuestion"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/topLayout"
    android:layout_weight="1" >




</WebView>

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtQuestion"
    android:paddingLeft="16dp"
    android:paddingRight="14dp" >

    <LinearLayout
        android:id="@+id/listViewQuestion"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    </LinearLayout>
   </ScrollView> 


 </RelativeLayout>

和我的Java代码:

 public class MainActivity extends Activity implements View.OnClickListener{

 private WebView webViewEquationDisplay,txtQuestion;
 private String mathML;

    private String doubleEscapeTeX(String s) {
    String t="";
    for (int i=0; i < s.length(); i++) {
        if (s.charAt(i) == '\'') t += '\\';
        if (s.charAt(i) != '\n') t += s.charAt(i);
        if (s.charAt(i) == '\\') t += "\\";
    }
    return t;
}

    @Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.examination_fragment_layout);
    txtQuestion = (WebView) findViewById(R.id.txtQuestion);
    txtQuestion.getSettings().setJavaScriptEnabled(true);

    try {
        InputStream raw = getApplicationContext().getAssets().open(
                "multipale_choose.xml");
        QuizzesParser parser = new QuizzesParser(this);
        parser.startParsing(raw);
        answerList=parser.getpList();
        initiateWebView(answerList.get(1).getAns_body(),txtQuestion);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
 private void initiateWebView( String formulaML,WebView formulaWebview){

    mathML="<p style='  height: 1000px;vertical-align: middle;text-align:center;'>";
    mathML=mathML+formulaML;

    mathML=mathML+"</p>";   

    webViewEquationDisplay=formulaWebview;
    webViewEquationDisplay.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){

                 webViewEquationDisplay.evaluateJavascript("javascript:document.getElementById('math').innerHTML='" +
                     doubleEscapeTeX(mathML)
                                  +"';",null);


            }
            else{
                webViewEquationDisplay.loadUrl("javascript:document.getElementById('math').innerHTML='" +
                        doubleEscapeTeX(mathML)+"';");
                        /*
                        "<font color=\"black\">`"+"<math xmlns=\"http://www.w3.org/1998/Math/MathML\">"
        +"<mstyle displaystyle=\"true\">"
                  +doubleEscapeTeX(e.getText().toString())
                  +"</mstyle></math>';"+"`</font>';");
                  */
            }

            webViewEquationDisplay.loadUrl("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);");
        }
    });

    final String mathJaxOfflineUrl = "file:///android_asset/MathJax/MathJax.js";            
    webViewEquationDisplay.loadDataWithBaseURL("http://bar/", "<script type='text/x-mathjax-config'>"
            +"MathJax.Hub.Config({ " 
                +"showMathMenu: false, "
                +"jax: ['input/MathML','output/HTML-CSS'], " 

                         +"extensions: ['mml2jax.js'], " 
                          +"TeX: { extensions: ['noErrors.js','noUndefined.js'] }, "

              +"});</script>"
            +"<script type='text/javascript' "
              +"src='"+mathJaxOfflineUrl+"'"
              +"></script><span id='math'></span>","text/html","utf-8","");

  }

 }

我正在关注此example

1 个答案:

答案 0 :(得分:2)

您使用line-height样式:

mathML = "<p style='line-height:400px; vertical-align: middle; text-align: center;'>";