Android setLogo未放置在Action Bar的中心

问题描述:

嗨,我正在使用工具栏并尝试将徽标设置为操作栏的中心。但在中心有标题和(我认为)这就是为什么标志从动作栏的左侧开始。然后我删除了标题以将徽标置于中间位置,但它没有任何好处。我该如何解决这个问题?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 –

试试这个:

<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

尝试这种方式

<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

工具栏只是一个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