我試圖在具有導航抽屜的應用程序中實現工具欄。現在我試圖將我的工具欄的標題放在所有片段的中間。我的問題是,我有時在導航抽屜裏有一個漢堡圖標右側的圖標,我在工具欄的左側有一個應用程序圖標。爲了使這些額外的圖標可見,我必須使用相對佈局,否則左側的應用程序圖標將會丟失。中心工具欄標題自定義工具欄中的圖標和導航抽屜切換按鈕
我的工具欄活動的XML看起來是這樣的:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context="app.activities.HomeActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/home_toolbar"
android:minHeight="?attr/actionBarSize"
android:background="@color/white"
app:contentInsetStartWithNavigation="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/search_toolbar_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:src="@drawable/icon_search_hightlighted" />
<ImageView
android:id="@+id/refresh_toolbar_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:src="@drawable/icon_refresh"
android:visibility="gone"/>
<TextView
android:id="@+id/actionbar_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/action_bar_title_home"
android:gravity="center"
android:textColor="@color/black"
android:textSize="18sp" />
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:scaleType="centerCrop"
android:layout_alignParentRight="true"
android:src="@drawable/app_logo" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<fragment
android:id="@+id/navigation_drawer"
android:name="app.fragments.NavigationDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_marginTop="?attr/actionBarSize"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
我設定的工具欄圖標的可視性左側取決於所選擇的片段的消失或可見。 有誰知道如何能夠居中工具欄標題,並仍然可以在我的工具欄中看到我的ImageViews?
在此先感謝。
編輯
我的工具欄標題是不是在目前的中心。它的定位像這樣一個問題: ToolBar title doesn't center
我已經讀過,這將有可能居中如果相對佈局會消失,並設置TextView的佈局重力CENTER_HORIZONTAL但這不起作用對我來說是因爲我的App的ImageView不會出現在工具欄的右側。
我編輯的問題,增加了一個鏈接到另一個問題,這有點兒顯示了我的標題如何定位。這是中心右側的一點。 – Distra
你有沒有嘗試過使用FrameLayout呢?也許這會給你控制你需要使用的方法,你提到你有它的工作,但右側的圖像消失。 –