我正在研究一个应用程序,当您按下按钮时会生成随机引号(来自开关)。然后进入下一个屏幕,然后将其显示为textView。该部分起作用。我想要一个按钮,这会将用户带回主屏幕(我还打算另外有两个按钮,但这些按钮稍后会出现)。但是,该应用只显示文本或按钮,而不是两者。如何显示文本和按钮? (android)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comp_out);
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);
Button button = new Button(this);
button.setText("Main Menu");
button.setTextColor(Color.parseColor("#ff0000"));
button.setHeight(75);
// Set the text view as the activity layout
setContentView(textView);
setContentView(button);
}
这是我用来创建文本输出和按钮的代码。字符串消息是引用。此代码正在制作一个填充整个屏幕的按钮。
<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:background="@color/orange">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginEnd="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:textColor="@color/blue"
android:text="@string/button_menu" />
</LinearLayout>
没有java代码使得一个按钮,我用这一点,这是不使按钮任一。 (也没有将背景设置为橙色,但那不是什么大问题)关于如何将按钮放在文本下的任何想法?