2015-04-01 16 views
1

我在android中創建一個imageview和textview的HTML代碼的幫助,所以我的問題是如何對齊文本,當我點擊閱讀更多,然後將打開新的頁面,所以..這裏是我的完整源code.can誰能幫我please.thanks提前如何在Android中使用html代碼對齊文本

MainActivity

public class MainActivity extends Activity { 

private final String htmlText = "<body><h1>Heading Text</h1><p>We understand that different client have different needs and requirements therefore special effort might be required to fulfill these requirements. Our custom software development services are aimed to fulfill......Read More &nbsp;</p>" + 

      "<blockquote>Example from <a href=\"www.javatechig.com\">" + 
      "Javatechig.com<a></blockquote></body>"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      TextView htmlTextView = (TextView)findViewById(R.id.html_text); 
      htmlTextView.setText(Html.fromHtml(htmlText)); 

} 
} 

我的佈局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 

     tools:context=".MainActivity" 
     > 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="35dp" 
      android:layout_height="35dp" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:src="@drawable/home" /> 

     <TextView 
      android:id="@+id/html_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_marginLeft="5dp" 
      android:layout_toRightOf="@+id/imageView1" 
      /> 

    </RelativeLayout> 

回答

0

要對齊下面的圖片文字刪除佈局

android:layout_alignParentTop="true" 
android:layout_toRightOf="@+id/imageView1" 

從文本視圖以下行,並增加以下行

android:layout_below="@+id/imageView1" 

隨着網頁視圖

與下面的代碼

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="35dp" 
     android:layout_height="35dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/ic_launcher" /> 

    <WebView 
     android:id="@+id/webview" 
     android:layout_below="@id/imageView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
</RelativeLayout> 

而且使用下面的代碼在你的活動

private final String htmlText = "<body><h1>Heading Text</h1><p>We understand that different client have different needs and requirements therefore special effort might be required to fulfill these requirements. Our custom software development services are aimed to fulfill......Read More &nbsp;</p>" + 

      "<blockquote>Example from <a href=\"http://www.javatechig.com\">" + 
      "Javatechig.com<a></blockquote></body>"; 

    private WebView m_webview; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     m_webview = (WebView) findViewById(R.id.webview); 
     m_webview.setWebViewClient(new WebViewClient() { 
      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) 
      { 
       if(url.equalsIgnoreCase("http://www.javatechig.com/")) { 
        Toast.makeText(MainActivity.this, url, Toast.LENGTH_SHORT).show(); 
        return true; 
       } 
       return super.shouldOverrideUrlLoading(view, url); 
      } 
     }); 
     m_webview.loadDataWithBaseURL("", htmlText, "text/html", HTTP.UTF_8, null); 
    } 
+0

我添加了此代碼,但所有文本也包含以下textview .... – 2015-04-01 08:52:11

+0

對不起,你不能請你提供提供更多的細節? – 2015-04-01 09:06:51

+0

例如我有一個像因此,當我點擊閱讀更多,然後將打開新的android佈局活動....先生可以ü指導我請 – 2015-04-01 11:02:25

0

嘗試在您的XML中爲您的TextView添加android:gravity="center",如果您想要將文本對齊到它的中心。

要打開網頁試試:htmlTextView.setMovementMethod(LinkMovementMethod.getInstance());

+0

我瓦納對齊側 – 2015-04-01 07:53:31

+0

遠低於圖像文本,你的XML看起來不錯替換佈局。你的文字在這個時候位於哪裏? – 2015-04-01 08:01:33

+0

我的文本位於圖像的右側.. – 2015-04-01 08:45:45