Android 如何在垂直中心显示webview数据
我希望下面的公式显示布局中的垂直中心。它水平居中。但它被展示在顶部,而不是中央 以下是我想要实现的目标 但这是它在顶部的表现: 以下是我的XML代码:Android 如何在垂直中心显示webview数据,android,android-webview,mathjax,Android,Android Webview,Mathjax,我希望下面的公式显示布局中的垂直中心。它水平居中。但它被展示在顶部,而不是中央 以下是我想要实现的目标 但这是它在顶部的表现: 以下是我的XML代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/title" android:lay
<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","");
}
}
public类MainActivity扩展活动实现View.OnClickListener{
私有WebView webViewEquationDisplay,txtQuestion;
私有字符串mathML;
专用字符串doubleEscapeTeX(字符串s){
字符串t=“”;
对于(int i=0;i”;
mathML=mathML+公式化ML;
mathML=mathML+“”;
webViewEquationDisplay=公式化WebView;
webViewEquationDisplay.setWebViewClient(新的WebViewClient(){
@凌驾
公共void onPageFinished(WebView视图,字符串url){
super.onPageFinished(视图、url);
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.KITKAT){
webViewEquationDisplay.evaluateJavascript(“javascript:document.getElementById('math')。innerHTML=”)+
doubleEscapeTeX(mathML)
+“;”,空);
}
否则{
webViewEquationDisplay.loadUrl(“javascript:document.getElementById('math')。innerHTML=”)+
doubleEscapeTeX(mathML)+“;”;
/*
"`"+""
+""
+doubleEscapeTeX(例如getText().toString())
+"';"+"`';");
*/
}
webviewequalationdisplay.loadUrl(“javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);”;
}
});
最终字符串mathJaxOfflineUrl=”file:///android_asset/MathJax/MathJax.js";
webViewEquationDisplay.loadDataWithBaseURL(“http://bar/", ""
+“MathJax.Hub.Config({”
+showMathMenu:错误
+“jax:['input/MathML','output/HTML-CSS'],”
+“扩展:['mml2jax.js'],”
+TeX:{extensions:['noErrors.js','noUndefined.js']}
+"});"
+“,”文本/html“,”utf-8“,”);
}
}
我遵循这一点。您使用的是
线条高度
样式:
mathML = "<p style='line-height:400px; vertical-align: middle; text-align: center;'>";
mathML=“”;
doandroid:centerInParent=“true”
当我使用android:centerInParent=“true”时,shw此消息错误:在包“android”中找不到属性“centerInParent”的资源标识符,修复了语法错误和打字错误。