1

我创建了一个简单的 android 应用程序,就像 google 教程中的那个。第一个活动上的按钮从文本字段中获取文本......然后使用第一个活动中输入的文本加载第二个活动。

我要做的就是向第二个活动添加一些简单的 UI 元素。在 xml 中我添加了一个按钮,我可以在图形 xml 查看器中很好地看到它,但是当我实际启动应用程序时,屏幕是空的。

在你提出建议之前......我已经尝试过这两个

<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/ratingBar1" android:layout_alignLeft="@+id/ratingBar1" android:text="Button" />

和这个

<Button android:id="@+id/button1" android:layout_width="100dp" android:layout_height="50dp" android:layout_above="@+id/ratingBar1" android:layout_alignLeft="@+id/ratingBar1" android:text="Button" />

两者似乎都没有任何区别。

有什么建议么?

编辑:

下面的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=".SecondScreen" >

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



<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="82dp"
    android:text="Button" />

</RelativeLayout>`

编辑2:

package com.example.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class SecondScreen extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second_screen);





        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        // Set the text view as the activity layout
        setContentView(textView);








    // Show the Up button in the action bar.
    setupActionBar();
}

/**
 * Set up the {@link android.app.ActionBar}.
 */
private void setupActionBar() {

    getActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.second_screen, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

4

4 回答 4

2

我正在提交另一个答案,因为我的第一个答案几乎没有。使用您的代码很容易发现问题。

您正在创建一个新的View被调用textView,然后将整个活动的布局设置为仅TextView使用setContentView(textView). 把它排除在外,不要以这种方式进行文本视图,而是参考您在布局中分配的 id。所以做类似的事情

TextView textBox = (TextView) findViewById(R.id.textView1);
textBox.setTextSize(40);
textBox.setText(message);

应该可以解决您的问题。

作为旁注,我不建议使用,intent.getStringExtra而是在您的第一个活动中使用一些静态方法public static getMessage()来返回您在第一个活动中private static String message分配和确定的变量。在您的第二个活动中,您将通过执行类似的操作来调用该方法String message = Class1Name.getMessage()。这些不应该是你的问题的根源,因为我真的不知道你在做什么的细节,我不能确定这样做是否会奏效。但无论如何,我几乎可以保证摆脱setContentView(textView)会让你的按钮正确显示。

于 2013-06-19T20:10:17.510 回答
0

setContentView(R.layout.your_layout);在你的onCreate(..)方法中添加了吗?

于 2013-06-18T18:54:55.020 回答
0

检查你的布局方向是否是垂直的,如果仍然没有帮助你在这里发布完整的 xml,有人可以在这里

于 2013-06-19T05:43:29.963 回答
0

更多的是评论而不是“答案”本身,但可能有两件事。可能是您的模拟器所基于的 android 设备的像素密度与 XML 查看器中的像素密度不同,因此由于marginTop,您可以尝试摆脱边缘和看看是否出现。如果这没有帮助,请将您的第二个活动的 ( SecondScreen) 代码放在上面,因为 XML 看起来应该可以正常工作。

于 2013-06-19T16:49:35.213 回答