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

但问题并没有解决。


当前回答

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

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

其他回答

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

在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 ());

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

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

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

截图:

对于那些在设置SupportActionBar (@sorianiv)后需要通过工具栏设置标题的人来说,你的工具栏在一个折叠工具栏布局中,请阅读以下内容:

mToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
Toolbar toolbar = findViewById(R.id.toolbar);
//toolbar.setTitle(""); // no need to do this
//mToolbarLayout.setTitle("Title"); // if you need an initial title, do this instead
setSupportActionBar(toolbar);

之后,

mToolbarLayout.setTitle("New 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()被调用之前。

如果你的目标是在工具栏中设置一个静态字符串,最简单的方法是在AndroidManifest.xml中设置活动标签:

<activity android:name=".xxxxActivity"
          android:label="@string/string_id" />

工具栏不需要任何代码就可以得到这个字符串。(适用于v27库。)