2014-07-18 42 views
3

我正在爲一所大學建設應用程序。根據需要,我將爲每個段落添加採用單獨TextView的段落。有沒有一種方法可以將段落組合在一起。它只是我的想法,這是否可能?這是我工作的代碼片段。如何在活動中提供段落?

<TextView 
android:id="@+id/textView2" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:inputType="textPostalAddress" 
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry." /> 

<TextView 
android:id="@+id/textView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignLeft="@+id/textView2" 
android:layout_below="@+id/textView2" 
android:layout_marginTop="14dp" 
android:inputType="numberSigned" 
android:text="+91 12 12345678" /> 

<TextView 
android:id="@+id/textView3" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignLeft="@+id/textView1" 
android:layout_below="@+id/textView1" 
android:inputType="phone" 
android:text="+91 12 12345678" /> 

<TextView 
android:id="@+id/textView4" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignLeft="@+id/textView3" 
android:layout_below="@+id/textView3" 
android:autoLink="email" 
android:inputType="textEmailAddress" 
android:text="[email protected]" /> 

在這裏,在我的代碼,我想給兩個電話號碼和電子郵件ID組合在單個TextView中。可能嗎?

+1

爲什麼不用'WebView'或'TextView'和'Html'格式的文本?它支持段落和更多標籤。然後它不需要爲每個段落單獨的Textview。 – user370305

+0

@ user370305,「WebView」,OK我會用。但如何處理Html格式的文本?指導我,我是Android的新手。 – Web

回答

4

試試這種方式,希望這會幫助你解決你的問題。

main.xml中

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

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 

MyActivity.java

public class MyActivity extends Activity{ 

    private TextView textView; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     textView = (TextView) findViewById(R.id.textView); 
     String htmlText = "<p> +91 12 12345678 </p> <p> +91 12 12345678 </p> <p> [email protected] </p>"; 
     textView.setText(Html.fromHtml(htmlText)); 
    } 
} 
+0

謝謝。它工作正常。但對齊錯過了。我怎樣才能做到這一點?如果我有冗長的段落呢?我能做同樣的事嗎? .java文件不會變得笨拙嗎? – Web

+0

如果您瞭解有關html代碼的更多信息,您可以輕鬆處理更多問題。 –

1

我不相信有一種方法可以在XML中做到這一點,只有可能使用多個文本視圖...你可以用html轉換以編程方式做到這一點。

此外,您應該從每個刪除android:inputType =「」,它們是用於EditText而不是TextView。

+0

謝謝。我接受你的答案 - android:inputType =「」 – Web

0

在我看來,最好是使用一個TextView的,並給它一個機器人:SRC =「@字符串/ your_string「屬性。

然後進入水庫 - >值 - >串並添加:

<string name="your_string">Everything you want to display write it here</string>  

希望我幫助。

+0

這對段落沒有幫助,所以它更像是評論而不是答案。 – Keelan