我在用

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

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

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


当前回答

我使用的是Android Studio 3.0.1。

有一个活动:

setTitle("Title Text");

在一个片段内:

getActivity().setTitle("Title Text");

其他回答

在Kotlin中,这样做:

this.title = resources.getText(R.string.fast_learning)

有个更快的方法,用

YourActivity.setTitle("New Title");

你也可以在onCreate()中找到它,例如:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.setTitle("My Title");
    }

顺便说一下,您根本不能在不传递任何Activity对象的情况下以静态方式调用setTitle()。

如果你有多个活动,你可以在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>

试试setTitle本身,像这样:

setTitle("Hello StackOverflow");

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

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