0

我在从主要活动接收字符串的活动中有以下代码:

String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
 TextView textView = new TextView(this);
 textView.setTextSize(20);
 textView.setText(message);
// Set the text view as the activity layout
 setContentView(textView);

我想在下面添加一张图片TextView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" >

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

     <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/serious" />    
</LinearLayout>

随着线setContentViewTextView占据了整个屏幕。没有线,图像显示,但字符串显示“ false”。任何想法?

4

2 回答 2

0

称呼

setContentView(R.layout.yourLayout);

接着

String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = (TextView)findViewById(R.id.textView); 
textView.setTextSize(20);
textView.setText(message);

它会起作用。

于 2013-07-05T10:54:13.953 回答
0

你做错了,而不是设置 setContentView(textView); 你应该使用 setContentView(R.layout.main);

并创建 main.xml 是 res/layout 上面提到的片段,如

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

    <ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/serious" />    

于 2013-07-05T10:37:05.223 回答