AndroidアプリではToolbar.setTitleメソッドは効果がありません。アプリケーション名がタイトルとして表示されます。質問する

AndroidアプリではToolbar.setTitleメソッドは効果がありません。アプリケーション名がタイトルとして表示されます。質問する

android-support-v7:21 ライブラリを使用して簡単なアプリケーションを作成しようとしています。

コード スニペット:
MainActivity.java

public class MainActivity extends ActionBarActivity {

    Toolbar mActionBarToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);            
        mActionBarToolbar.setTitle("My title");
        setSupportActionBar(mActionBarToolbar);
}

アクティビティメイン.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar            
        android:id="@+id/toolbar_actionbar"
        android:background="@null"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:fitsSystemWindows="true" />

</LinearLayout>

しかし、ツールバーに「My title」の代わりに %application name% が表示されます。この方法は効果がないようです。 「My title」を表示したい
と思います。setTitle

UPD:以前styles.xmlは:

<style name="AppTheme" parent="Theme.AppCompat">
    <item name="windowActionBar">false</item>
</style>

そこで、アクションバーは使用されていないと考えました。NoActionBarスタイルの親に以下を追加します。

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="windowActionBar">false</item>
</style>

しかし、問題は解決されていません。

ベストアンサー1

解決策を見つけました:

の代わりに:

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);            
mActionBarToolbar.setTitle("My title");
setSupportActionBar(mActionBarToolbar);

私が使用したのは:

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);            
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("My title");

そしてそれは機能します。

おすすめ記事