Android:setContentView占用整个屏幕

时间:2013-07-05 10:07:06

标签: android textview imageview

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

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”。有什么想法吗?

2 个答案:

答案 0 :(得分: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" />    

答案 1 :(得分: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);

它会起作用。