簡體   English   中英

使用工具欄中的自定義標題和后退按鈕

[英]use custom title in toolbar with back button

我想在工具欄標題中使用自定義文本。 我已將其添加如下

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

它工作正常,但我在工具欄中獲得自定義標題文本以及應用程序名稱。 我只想要我的自定義文字。 如何刪除默認標題並僅使用工具欄中的自定義文本?

謝謝

當您在單獨的TextView中設置標題時,您需要刪除“普通”標題

getSupportActionBar().setTitle("");

請使用此代碼:

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

希望能幫助到你,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM