我在用

Window w = getWindow();
w.setTitle("My title");

要更改我当前活动的标题,但似乎不工作。

有人能告诉我怎么改变吗?


当前回答

我有一个工具栏在我的活动和基础活动,覆盖所有的标题。所以我必须在onResume()在活动中使用setTitle,就像这样:

@Override
  protected void onResume() {
    super.onResume();
    toolbar.setTitle(R.string.title);
  }

其他回答

setTitle(“应用程序”); 在MainActivity.java中更简单 我会说。

setTitle(getResources().getText(R.string.MyTitle));

试试setTitle本身,像这样:

setTitle("Hello StackOverflow");

仅供参考,您可以选择从XML执行此操作。

在AndroidManifest.xml中,你可以用

android:label="My Activity Title"

Or

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>

这对我很管用。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment, container, false);
    getActivity().setTitle("My Title");
//...
}