2017-07-14 66 views
1

嗨,我正在使用工具欄並嘗試將徽標設置爲操作欄的中心。但在中心有標題和(我認爲)這就是爲什麼標誌從動作欄的左側開始。然後我刪除了標題以將徽標置於中間位置,但它沒有任何好處。我該如何解決這個問題?Android setLogo未放置在Action Bar的中心

這裏是我用來設置工具欄的方法。

private void setToolBar(){ 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayShowTitleEnabled(false); 
     toolbar.setBackgroundColor(getResources().getColor(colorActionBar)); 
     toolbar.setLogo(R.drawable.logoactionbar); 

    } 

這裏是我的清單

android:theme="@style/Theme.AppCompat.Light.NoActionBar"> 

主題,這裏是工具欄的decleration

 <<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/colorActionBar" 
    android:fitsSystemWindows="true" 
    tools:context="com.anadolusigorta.rhextranet.com.anadolusigorta.rhextranet.HomeActivity"> 

    <View 
     android:id="@+id/statusBarBackground" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:elevation="0dp" 
     android:background="@color/colorActionBar" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="@color/colorActionBar" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 
     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/logoactionbar" 
      android:layout_gravity="center" 
      android:id="@+id/toolbar_title" /> 

    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_home" /> 

</android.support.design.widget.CoordinatorLayout> 
+0

你創建一個自定義工具欄 –

+0

您可能需要使用ReleativeLayout代替CoordinatorLayout –

回答

1

試試這個:

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    app:popupTheme="@style/AppTheme.PopupOverlay"> 

    <LinearLayout 
     android:gravity="center" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <ImageView 
      android:src="@android:drawable/btn_star_big_off" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 



    </LinearLayout> 

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

當我做這個標誌不shown.When我做的這個標誌不顯示。我是usign協調員佈局,我正在更新應用程序欄的xml decleration – Cem

+0

更改我的xml這工作後,我接受它作爲答案 – Cem

0

嘗試這種方式

<android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/AppTheme.AppBarOverlay"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary"> 

       <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/your_logo" 
        android:layout_gravity="center" 
        android:id="@+id/toolbar_title" /> 

      </android.support.v7.widget.Toolbar> 
     </android.support.design.widget.AppBarLayout> 
+0

當我這樣做的標誌不顯示。我是usign協調員佈局,我正在更新應用欄的xml decleration – Cem

0

工具欄只是一個ViewGroup,您可以自定義它。

試試這個:

<android.support.v7.widget.Toolbar 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@color/color_primary" 
     android:minHeight="@dimen/abc_action_bar_default_height_material"> 

    <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:src="@drawable/ic_launcher"/> 

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

這似乎是正確的方法,但沒有給出正確的結果。標誌放置在下面 – Cem