我在用

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

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

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


当前回答

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

其他回答

这对我很管用。

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

试试setTitle本身,像这样:

setTitle("Hello StackOverflow");

如果你想改变活动标题时,你改变活动点击按钮。在MainActivity中声明必要的变量:

    private static final String TITLE_SIGN = "title_sign";
    ImageButton mAriesButton;

在onCreate()中添加onClickListener,并为另一个活动创建新意图:

    mTitleButton = (ImageButton) findViewById(R.id.title_button);
    mTitleButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(MainActivity.this, 
        SignActivity.class);
        String title_act = getText(R.string.simple_text).toString();
        intent.putExtra("title_act", title_act);
        startActivity(intent);
        finish();
        }
    });

onCreate()中的SecondActivity代码:

    String txtTitle = getIntent().getStringExtra("title_act");
    this.setTitle(txtTitle);

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

如果你想在Java文件中设置标题,那么写在你的活动onCreate

setTitle("Your Title");

如果你想在Manifest里写

    <activity
        android:name=".MainActivity"
        android:label="Your Title" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>