如何在 Intent 中设置 Activity 标题?
how to set Activity Title in Intent?
在TabHost
中我们可以做到getActionBar().setTitle("ACTITIVTY TITLE")
。
但是 intent
中呢?
是否可以在 intent
中设置标题?
代码
Intent i = new Intent(MenuActivity.this, DrawerListActivity.class);
startActivity(i);
Toast.makeText(getBaseContext(), "RF number: " +
KEY_RFnumber, Toast.LENGTH_SHORT).show();
不能直接用intent设置标题。您可以传递意图中的标题,并让您的目标 activity 从意图中提取标题并进行设置。这只是目标 activity.
中的几行额外代码
MainActivity.class
public void send(View view) {
Intent intent = new Intent(this, DrawerListActivity.class);
String message = "Drawer Title";
intent.putExtra("key", message);
startActivity(intent);
}
DrawerListActivity.class,在onCreate()
String message = getIntent().getStringExtra("key").toString(); // Now, message has Drawer title
setTitle(message);
现在,将此消息设置为标题。
我明白了!我只是从我的 MainActivity 声明一个 public 静态字符串,它包含一个我在 Intent 中设置的字符串,然后它调用我的 DrawerListActivity。它完美地工作!谢谢。
我只是粘贴另一个人关于这个问题的答案可能会对你有所帮助
<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>
在TabHost
中我们可以做到getActionBar().setTitle("ACTITIVTY TITLE")
。
但是 intent
中呢?
是否可以在 intent
中设置标题?
代码
Intent i = new Intent(MenuActivity.this, DrawerListActivity.class);
startActivity(i);
Toast.makeText(getBaseContext(), "RF number: " +
KEY_RFnumber, Toast.LENGTH_SHORT).show();
不能直接用intent设置标题。您可以传递意图中的标题,并让您的目标 activity 从意图中提取标题并进行设置。这只是目标 activity.
中的几行额外代码MainActivity.class
public void send(View view) {
Intent intent = new Intent(this, DrawerListActivity.class);
String message = "Drawer Title";
intent.putExtra("key", message);
startActivity(intent);
}
DrawerListActivity.class,在onCreate()
String message = getIntent().getStringExtra("key").toString(); // Now, message has Drawer title
setTitle(message);
现在,将此消息设置为标题。
我明白了!我只是从我的 MainActivity 声明一个 public 静态字符串,它包含一个我在 Intent 中设置的字符串,然后它调用我的 DrawerListActivity。它完美地工作!谢谢。
我只是粘贴另一个人关于这个问题的答案可能会对你有所帮助
<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>