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

但问题并没有解决。


当前回答

这不仅仅是关于.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()

其他回答

这是因为你同时使用了工具栏和动作栏。现在,当你想使用工具栏作为一个操作栏,你需要做的第一件事是禁用装饰提供的操作栏。

最简单的方法是让你的主题从theme . appcompat . noactionbar扩展。

我尝试从片段重命名工具栏

它帮助了我,我希望能帮助其他人

Activity activity = this.getActivity();
Toolbar toolbar = (Toolbar) activity.findViewById(R.id.detail_toolbar);
        if (toolbar != null) {
            activity.setTitle("Title");
        }

//toolbar.setTitle("Title"); did not give the same results

截图:

虽然不是立即相关的这个特定的设置,我发现,从我的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()被调用之前。

我有个奇怪的行为也许能帮到你。 这是工作,但它没有影响onCreate只:

toolbar.setTitle("title");

试着在onCreate中使用这个:

yourActivityName.this.setTitle("title")

对于任何在设置SupportActionBar后需要通过工具栏设置标题的人,请阅读本文。

支持库的内部实现只是在设置SupportActionBar时检查工具栏是否有标题(不是空)。如果有,则将使用此标题而不是窗口标题。然后,您可以在加载真实标题时设置一个虚拟标题。

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

后来……

mActionBarToolbar.setTitle(title);