使用工具栏中的自定义标题和后退按钮

时间:2017-10-04 19:36:39

标签: java android android-toolbar android-appbarlayout

我想在工具栏标题中使用自定义文字。我已将其添加如下

<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);
        }

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

由于

2 个答案:

答案 0 :(得分:2)

当您在单独的TextView中设置标题时,您需要通过

删除“普通”标题
<a>

答案 1 :(得分:0)

请使用此代码:

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

希望它有所帮助,

相关问题