我创建了一个简单的 Android 应用程序,就像谷歌教程中的应用程序一样。第一个 Activity 上的按钮从文本字段中获取文本……然后使用在第一个 Activity 中输入的文本加载第二个 Activity 。
我想要做的就是向第二个 Activity 添加一些简单的 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);
}
最佳答案
我正在提交另一个答案,因为我的第一个答案几乎不是答案。使用您的代码很容易发现问题。
您正在创建一个名为 textView
的新 View
,然后将整个 Activity 的布局设置为只有 TextView
和 设置内容 View ( TextView )
。将其排除在外,而不是以这种方式进行 TextView ,而是引用您在布局中分配的 id。所以做一些像
TextView textBox = (TextView) findViewById(R.id.textView1);
textBox.setTextSize(40);
textBox.setText(message);
那应该解决您的问题。
作为旁注,我不建议使用 intent.getStringExtra
,而是在您的第一个 Activity 中使用一些静态方法 public static getMessage()
来返回您在第一个 Activity 中分配和确定的 private static String message
变量。在您的第二个 Activity 中,您可以通过执行类似 String message = Class1Name.getMessage()
的操作来调用该方法。这些不应该是你问题的根源,因为我真的不知道你在做什么的细节,我不能肯定地说这样做是否会奏效。但无论如何,我几乎可以保证摆脱 setContentView(textView)
将使您的按钮正确显示。
关于android UI 元素没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17176631/