如何在Android中更改活动的标题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2198410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change title of Activity in Android?
提问by UMAR
I am using
我在用
Window w = getWindow();
w.setTitle("My title");
to change title of my current Activity but it does not seem to work.
更改我当前活动的标题,但它似乎不起作用。
Can anyone guide me on how to change this?
谁能指导我如何改变这一点?
回答by jeffh
Try setTitle by itself, like this:
单独尝试 setTitle,如下所示:
setTitle("Hello StackOverflow");
回答by BullShark
Just an FYI, you can optionally do it from the XML.
仅供参考,您可以选择从 XML 执行此操作。
In the AndroidManifest.xml, you can set it with
在 AndroidManifest.xml 中,你可以设置它
android:label="My Activity Title"
Or
或者
android:label="@string/my_activity_label"
Example:
例子:
<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>
回答by hB0
If you want it one time & let system handle the rest (not dynamic) then do like this in your manifest file:
如果你想要一次并让系统处理其余的(不是动态的),那么在你的清单文件中这样做:
<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>
回答by Gennady Kozlov
setTitle(getResources().getText(R.string.MyTitle));
回答by moberme
This worked for me.
这对我有用。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment, container, false);
getActivity().setTitle("My Title");
//...
}
回答by Defrag
There's a faster way, just use
有一个更快的方法,只需使用
YourActivity.setTitle("New Title");
You can also find it inside the onCreate()with this, for example:
您还可以在onCreate() 中找到它,例如:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTitle("My Title");
}
By the way, what you simply cannot do is call setTitle() in a static way without passing any Activity object.
顺便说一句,您根本无法做的是在不传递任何 Activity 对象的情况下以静态方式调用 setTitle() 。
回答by SGX
If you have multiple activities, you can set it like this in AndroidManifest.xml
如果你有多个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>
回答by adrian
I'm using Android Studio 3.0.1.
我正在使用 Android Studio 3.0.1。
WIth an Activity:
有活动:
setTitle("Title Text");
Inside a fragment:
在片段内:
getActivity().setTitle("Title Text");
回答by Govind Maheshwari
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Main_Activity);
this.setTitle("Title name");
}
回答by Rohan Gupta
The code helped me change the title.
代码帮助我更改了标题。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
ActivityName.this.setTitle("Your Activity Title");}