写入TextView对象时,文本不会出现在屏幕上

时间:2015-04-16 08:26:31

标签: android

主要活动onCreate:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     

    // Specify text size
    textView = new TextView(this);
    textView.setTextSize(10);
    setContentView(textView);

现在我可以使用textView.append()和我写的任何内容出现在屏幕上,很棒。

但是,如果我想使用这样的布局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"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/a_text_window"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp" />

</RelativeLayout>

我应该如何编码onCreate方法,以便我可以设置setContentView(R.layout.activity_main),然后以相同的方式使用textView.append()?我只想要一个带有文本的窗口,所以我可以稍后添加一些其他按钮。

1 个答案:

答案 0 :(得分:1)

尝试此操作来扩充main.xml文件并获取TextView。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     

    //Inflate your xml called main.xml
    setContentView(R.layout.main);
    TextView textView = (TextView) findViewById(R.id.a_text_window);
    // Specify text size (ALREADY DONE IN XML)
    textView.setTextSize(10);
}

顺便说一下,你可以在xml文件中定义你的文本大小! :)