我在用

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

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

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


当前回答

setTitle(“欢迎活动”);

其他回答

试试setTitle本身,像这样:

setTitle("Hello StackOverflow");

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

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

有个更快的方法,用

YourActivity.setTitle("New Title");

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

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

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

MainActivity内部:

public class act1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act1);

    setTitle("First Activity");

}

}