如何在android中编写段落

时间:2014-06-13 04:44:12

标签: android

我想在android应用程序中插入一个段落。 我的代码是这样的:

MainActivity:

public class MainActivity extends ActionBarActivity {

    TextView t;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView t = (TextView) findViewById(R.id.text);
        t.setText("This is the first line\nThis is the second line\nThird line...");
    }

activity_main.xml中

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.helloworld_5.MainActivity$PlaceholderFragment" >
    <TextView
        android:text="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>

不幸的是该应用程序无法运行。我还需要包含哪些内容?

请帮助..

2 个答案:

答案 0 :(得分:8)

将TextView的text属性更改为xml文件中的id ...

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

答案 1 :(得分:2)

这对我有用:

TextView t = (TextView) findViewById(R.id.textView);
t.setText(Html.fromHtml("<p>This is the first line</p>
               <p>This is the second line</p><p>Third line...</p>"));