2013-01-10 51 views
2

我在xml中有一個LinearLayout,現在我將webview添加到該LinearLayout,但是webview顯示在頂部而不在中間對齊。編程將webview對齊到LinearLayout的中心

這是它現在如何顯示。

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

現在它的垂直居中的佈局。

回答

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

感謝。

+0

沒有它沒有工作我試過 – Goofy

+0

@Goofy看到我的編輯我發佈了完整的代碼,這對我很好。 –

+0

我會嘗試只是一分鐘 – Goofy

0

試着在你的活動使用下面的代碼:

ansCellLayout.setGravity (Gravity.CENTER); 
-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> 
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); 

享受!