您可以在 xml 中声明所有布局和视图。对于给定的示例,代码如下所示
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set your parent view
setContentView(R.layout.main_layout);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Get the reference to the TextView and update it's content
TextView textView = (TextView)findViewById(R.id.my_text_view);
textView.setText(message);
}
你的 main_layout.xml 看起来像
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:id="@+id/my_text_view"/>