活動類,
public class MainActivity extends AppCompatActivity {
TextView tvStart, tvCreate, tvResume;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvCreate = (TextView)findViewById(R.id.tvCreate);
tvStart = (TextView)findViewById(R.id.tvStart);
tvResume = (TextView)findViewById(R.id.tvResume);
tvCreate.setText("Created");
}
@Override
protected void onStart() {
super.onStart();Started
tvStart.setText("Started");
}
@Override
protected void onPostResume() {
super.onPostResume();
tvResume.setText("Resumed");
}
}
XML對於這一點,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.technobd.stack.MainActivity"
tools:showIn="@layout/activity_main"
android:orientation="vertical">
<TextView
android:id="@+id/tvCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<TextView
android:id="@+id/tvStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<TextView
android:id="@+id/tvResume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
您還可以添加textviews動態像, Activity類會,
public class MainActivity extends AppCompatActivity {
LinearLayout llTextViews;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llTextViews = (LinearLayout)findViewById(R.id.llTextViews);
TextView tvCreate = new TextView(MainActivity.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
tvCreate.setLayoutParams(params);
tvCreate.setText("Created");
llTextViews.addView(tvCreate);
}
@Override
protected void onStart() {
super.onStart();
TextView tvStart = new TextView(MainActivity.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
tvStart.setLayoutParams(params);
tvStart.setText("Started");
llTextViews.addView(tvStart);
}
@Override
protected void onPostResume() {
super.onPostResume();
TextView tvResume = new TextView(MainActivity.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
tvResume.setLayoutParams(params);
tvResume.setText("Resumed");
llTextViews.addView(tvResume);
}
}
和xml佈局是,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.technobd.stack.MainActivity"
tools:showIn="@layout/activity_main"
android:orientation="vertical">
<LinearLayout
android:id="@+id/llTextViews"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
</LinearLayout>
爲什麼你一直在改變setContentView? – Brendon