使用Back按钮在工具栏中使用自定义标题



我想要工具栏标题中的自定义文本。我添加了下面的

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:titleTextColor="?attr/TextColor">
        <TextView
            android:textColor="?attr/TextColor"
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
            android:layout_gravity="center" />
        </android.support.v7.widget.Toolbar>

,在Java中,我编写了如下

的代码
toolbar = (Toolbar) findViewById(R.id.toolbar);
        Typeface font=Typeface.createFromAsset(AuthorQuoteActivityTabs.this.getAssets(), constant.font);
        TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
        mTitle.setText("authorName");
        mTitle.setTypeface(font,Typeface.BOLD);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

它的工作正常,但我在工具栏中获得了自定义标题文本以及应用程序名称。我只想要我的自定义文本。如何删除默认标题并仅在工具栏中使用自定义文本?

谢谢

当您在单独的文本视图中设置标题时,您需要通过

删除"正常"标题
getSupportActionBar().setTitle("");

请使用此代码:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("Toolbar Title");
    setSupportActionBar(toolbar);

希望它有帮助,

最新更新