Answers:
自己尝试setTitle,如下所示:
setTitle("Hello StackOverflow");
只是一个FYI,您可以选择从XML中进行操作。
在AndroidManifest.xml中,您可以使用
android:label="My Activity Title"
要么
android:label="@string/my_activity_label"
例:
<activity
android:name=".Splash"
android:label="@string/splash_activity_title" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
如果您想一次并让系统处理其余部分(不是动态的),请在清单文件中执行以下操作:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name_full" > //This is my custom title name on activity. <- The question is about this one.
<intent-filter android:label="@string/app_launcher_name" > //This is my custom Icon title name (launcher name that you see in android apps/homescreen)
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
setTitle(getResources().getText(R.string.MyTitle));
有一种更快的方法,只需使用
YourActivity.setTitle("New Title");
您还可以在onCreate()中找到它,例如:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTitle("My Title");
}
顺便说一句,您根本无法做的就是以静态方式调用setTitle()而不传递任何Activity对象。
如果您有多个活动,则可以在AndroidManifest.xml中进行如下设置
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NumbersActivity"
android:label="@string/category_numbers"
android:theme="@style/category_numbers" />
<activity
android:name=".FamilyActivity"
android:label="@string/category_family"
android:theme="@style/category_family" />
<activity
android:name=".ColorsActivity"
android:label="@string/category_colors"
android:theme="@style/category_colors" />
<activity
android:name=".PhrasesActivity"
android:label="@string/category_phrases"
android:theme="@style/category_phrases" />
<activity
android:name=".ExperimentActivity"
android:label="@string/category_experiment"
android:theme="@style/category_experiment" />
</application>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Main_Activity);
this.setTitle("Title name");
}
如果要在Java文件中设置标题,请在活动中写onCreate
setTitle("Your Title");
如果要在清单中写
<activity
android:name=".MainActivity"
android:label="Your Title" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
该代码帮助我更改了标题。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
ActivityName.this.setTitle("Your Activity Title");}
如果您想通过单击按钮更改活动时更改活动标题。在MainActivity中声明必要的变量:
private static final String TITLE_SIGN = "title_sign";
ImageButton mAriesButton;
在onCreate()中添加onClickListener并为另一个活动添加新意图:
mTitleButton = (ImageButton) findViewById(R.id.title_button);
mTitleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,
SignActivity.class);
String title_act = getText(R.string.simple_text).toString();
intent.putExtra("title_act", title_act);
startActivity(intent);
finish();
}
});
onCreate()中的SecondActivity代码:
String txtTitle = getIntent().getStringExtra("title_act");
this.setTitle(txtTitle);