我在用

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

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

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


当前回答

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

其他回答

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

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

在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()。

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

如果你想要它一次,让系统处理剩下的(不是动态的),那么在你的manifest文件中这样做:

<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>