Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何显示布局和消息?_Android_Android Studio - Fatal编程技术网

Android 如何显示布局和消息?

Android 如何显示布局和消息?,android,android-studio,Android,Android Studio,我创建了一个消息活动,它可以工作,但我的问题是我创建的xml布局没有显示消息,只有消息,反之亦然(布局显示时没有消息)。我如何解决这个问题 下面是该类的代码: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); String message = intent.getS

我创建了一个消息活动,它可以工作,但我的问题是我创建的xml布局没有显示消息,只有消息,反之亦然(布局显示时没有消息)。我如何解决这个问题

下面是该类的代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String message = intent.getStringExtra (MESSAGE_KEY);
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText (message);
        setContentView(textView);
        setContentView(R.layout.activity_fire_alert);
    }
这是我想展示的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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.user.tictactoeniandy.FireAlertActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="View Sender Location"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="91dp"
        android:id="@+id/loc_button1"/>

</RelativeLayout>

您只能有一个内容视图。设置的第二个将覆盖另一个。正确的方法是在布局中有一个文本视图,并将该视图上的文本设置为消息

setContentView()
方法,顾名思义,设置活动的内容。要解决此问题,请将
TextView
直接放在布局文件上,例如:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
...>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="
        ...
         .../>

    <TextView
        android:id="@+id/text_view_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40sp"
        android:layout_centerInParent="true"/>

</RelativeLayout>

您只能有一个集合内容视图。尝试用我的更新你的布局和代码,并检查它是否会同时显示这两个内容。

最佳答案。简明扼要。没有代码,只有方向。我只是编程新手,所以这真的很有用:)它很有效!那么,如果我添加另一个ImageView,我可以用同样的方法吗?谢谢你接受我的回答,是的,你可以用同样的方法来做,但我更希望你能阅读有关android开发的基础知识,并继续前进。
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fire_alert);

    TextView textView = (TextView) findViewById(R.text_view_message);

    Intent intent = getIntent();
    String message = intent.getStringExtra (MESSAGE_KEY);
    textView.setText (message);
}
<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" 
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context="com.example.user.tictactoeniandy.FireAlertActivity">

    <TextView
            android:id="@+id/loc_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="View Sender Location"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="View Sender Location"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="91dp"
            android:id="@+id/loc_button1"/>

    </LinearLayout>
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fire_alert);        
Intent intent = getIntent();
        String message = intent.getStringExtra (MESSAGE_KEY);
        TextView textView = (TextView)findviewbyid(R.id.loc_textview);
        textView.setTextSize(40);
        textView.setText (message);


    }