簡體   English   中英

工具欄將標題與后退按鈕對齊

[英]Toolbar align title in center with back button

我正在我的應用程序中添加Toolbar但似乎有問題。 在應用程序中,我應該有一個位於屏幕右側的按鈕,以及位於中心的標題。 但是當我深入應用程序時,我應該通過執行getSupportActionBar().setDisplayHomeAsUpEnabled()在左側顯示后退箭頭。 這工作正常,但是當我添加后退按鈕時,文本向右移動,因此我猜后退按鈕可以有一些空間。 有人能告訴我如何讓我的按鈕在右邊,標題總是在中心,不管是否顯示后退按鈕?

這是我的工具欄 xml 布局代碼:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:padding="5dp"
    tools:ignore="MissingPrefix">

    <RelativeLayout
        android:id="@+id/toolbar_info_holder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:textColor="@color/actionbar_title_color" />

        <ImageView
            android:id="@+id/toolbar_image"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />

        <TextView
            android:id="@+id/toolbar_count"
            android:layout_width="13dp"
            android:layout_height="13dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/red_circle"
            android:gravity="center"
            android:text="4"
            android:textColor="@color/white"
            android:textSize="9sp" />
    </RelativeLayout>

</android.support.v7.widget.Toolbar>

對我有用的是設置:

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"

到工具欄的子視圖,它對我來說是一個 LinearLayout(對你來說是一個 RelativeLayout)。 起初對我來說聽起來很奇怪,但它正在起作用,所以我不會抱怨。

首先,一些通用的提示。 這是 Android Hierarchical 視圖背后的確切目的。 使用它來確定后退按鈕的容器對象是什么,所有內容如何組合在一起,等等。

我懷疑您可以通過將某些wrap_content更改為match_parent來在一定程度上解決您的問題。 具體來說,我懷疑在您的RelativeLayoutlayout_width更改它會解決問題。 但即使不是這樣,也可以使用分層視圖來更好地了解您在玩耍時到底發生了什么,它應該有助於為您指明正確的方向。

您可能設置了活動標題,這會阻止徽標在操作欄上居中。

嘗試:

getActionBar().setDisplayShowTitleEnabled(false);

對於 v.7:

getSupportActionBar().setDisplayShowTitleEnabled(false);

這來自於這個問答 歸功於@Fillipo Mazza。

希望能幫助到你。

這篇文章有點晚了,但會解釋它是如何工作的。

您可以使用以下代碼為工具欄使用自定義視圖。

    supportActionBar?.setCustomView(R.layout.app_bar_title)
    supportActionBar?.displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM

基本上發生的是, app_bar_layout 被添加為工具欄內的自定義視圖。 因此,假設需要在中間顯示標題以及右側的后退鍵或操作按鈕。 我們可以像這樣創建布局。

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/app_bar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_gravity="center"
    tools:text="HOME" />

此文本視圖將附加在工具欄布局內,並在工具欄布局的中心對齊。 即使您添加返回鍵或操作菜單,它也應保留在中心。

暫無
暫無

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

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