我试图使用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);
}

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

但是工具栏上显示的不是“我的标题”%应用程序名称%。 似乎setTitle方法没有效果。 我想展示"我的头衔"

乌利希期刊指南: 在此之前,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>

但问题并没有解决。


当前回答

如果你使用的是折叠工具栏和工具栏,那么你需要在这两个布局中设置标题

在onCreate方法中将工具栏设置为操作栏

protected void setUpToolBar() {

    if (mToolbar != null) {
        ((HomeActivity) getActivity()).setSupportActionBar(mToolbar);
        mToolbar.setTitleTextColor(Color.WHITE);
        mToolbar.setTitle("List Detail");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity()
                        .getSupportFragmentManager().popBackStack();
            }
        });
        ((HomeActivity) getActivity()).getSupportActionBar()
                .setDisplayHomeAsUpEnabled(true);
    }
}

稍后使用setTitle方法简单地更新工具栏的标题

mToolbar .setTitle (productFromShoppingList.getProductName ()); mCollapsingToolbar.setTitle (productFromShoppingList.getProductName ());

其他回答

这不仅仅是关于.setTitle

onCreate中支持工具栏(Appcompat v7)的更多方法仅适用于

.method getSupportActionBar () ()

并且不使用mToolbar.method()

例子:

getSupportActionBar().setTitle("toolbar title");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

虽然next方法在onCreate中没有getSupportActionBar()工作很好

mToolbar.setVisibility(View.VISIBLE);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //
    }

问题只与onCreate事件,你仍然可以使用mToolbar.setTitle()之后,而不是恼人的getSupportActionBar().setTitle(),例如,如果你添加这个在onCreate它将工作(因为它将被执行后,onCreate)

mHandler.post(new Runnable() {
    @Override
    public void run() {
        mToolbar.setTitle("toolbar title");
    }
});

我更喜欢使用这个解决方案https://stackoverflow.com/a/35430590/4548520而不是https://stackoverflow.com/a/26506858/4548520,因为如果你改变标题很多次(在不同的函数),使用mToolbar.setTitle()比更长的getSupportActionBar().setTitle()一个,你不会看到讨厌的通知null异常像getSupportActionBar().setTitle()

只需在适配器中使用它, MainActivity是AppCompactActivity。 在任何地方都可以调用它。

((MainActivity) context).getSupportActionBar().setTitle(titles.get(position));
 getSupportActionBar().setTitle("Your Title");

虽然不是立即相关的这个特定的设置,我发现,从我的XML中删除“CollapsingToolbarLayout”是包装我的工具栏在一个AppBarLayout使一切工作。

所以,这个:

      <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways"
                app:navigationIcon="@drawable/ic_arrow_back_white_24dp" />

        </android.support.design.widget.AppBarLayout>

而不是这样:

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/collapsingToolbar"
                android:minHeight="?attr/actionBarSize"
                app:layout_scrollFlags="enterAlways|scroll|snap">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_height="?attr/actionBarSize"
                    app:navigationIcon="@drawable/ic_arrow_back_white_24dp" />
            </android.support.design.widget.CollapsingToolbarLayout>

        </android.support.design.widget.AppBarLayout>

然后,我在活动的onCreate中设置标题,在setSupportActionBar()被调用之前。

确保你添加了这个选项:

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);