Cómo centralizar ImageView en una barra de herramientas?

He estado tratando de centrar un logotipo dentro de mi barra de herramientas. Cuando navego a la siguiente actividad, el ícono "up affordance" está presente, y empujará mi logotipo ligeramente hacia la derecha. ¿Cómo puedo mantener mi logotipo en el centro de la barra de herramientas, sin quitar el icono de asequibilidad?

Esta es mi etiqueta de barra de herramientas

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/primaryColor" android:paddingTop="@dimen/app_bar_top_padding" app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" app:theme="@style/CustomToolBarTheme"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/ic_android_logo" android:layout_gravity="center" android:paddingBottom="3dp"/> </android.support.v7.widget.Toolbar> 

En javadoc , dice:

Una o varias vistas personalizadas. La aplicación puede agregar vistas secundarias arbitrarias a la barra de herramientas. Aparecerán en esta posición dentro del diseño. Si Toolbar.LayoutParams de una vista secundaria indica un valor de gravedad de CENTER_HORIZONTAL, la vista intentará centrarse dentro del espacio disponible restante en la barra de herramientas después de que se hayan medido todos los demás elementos .

Esto significa que no puede hacerlo a menos que cree una toolbar personalizada o quite el icon .

Usted puede hacerlo como siguiente que funciona para mí:

 <android.support.v7.widget.Toolbar android:id="@+id/mainToolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:minHeight="@dimen/abc_action_bar_default_height_material"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/headerLogo" android:contentDescription="@string/app_name" /> </RelativeLayout> </android.support.v7.widget.Toolbar> 

Establezca la imagen como un fondo de la barra de herramientas, no como una vista secundaria.

 <android.support.v7.widget.Toolbar android:id="@+id/detail_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?android:attr/listPreferredItemHeight" android:background="@drawable/my_image" app:theme="@style/CustomToolBarTheme"> 

Usted puede hacer por programación, que es la única manera que he logrado centrar completamente un logotipo en la barra de herramientas.

Actividad Añadir a usted:

 @Override public void onWindowFocusChanged(boolean hasFocus){ // set toolbar logo to center programmatically Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar); ImageView logo = (ImageView) findViewById(R.id.logo); int offset = (toolbar.getWidth() / 2) - (logo.getWidth() / 2); // set logo.setX(offset); } 

Y tu toolbar_main.xml:

 <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/main_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/bg_yellow" android:elevation="4dp" android:fontFamily="@string/font_family_thin" app:contentInsetStartWithNavigation="0dp" android:layout_gravity="center_horizontal"> <TextView android:id="@+id/title_toolbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="@string/font_family_light" android:textColor="@color/text_dark_xxx" android:textSize="@dimen/text_default" android:layout_centerVertical="true" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/logo" android:id="@+id/logo" android:paddingTop="5dp" /> </android.support.v7.widget.Toolbar> 

Para los desarrolladores que utilizan imágenes vectoriales o SVG .
Este puede ser tu fondo para la barra de herramientas, drawable_toolbar_background.xml

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="@color/primary"/> </shape> </item> <item android:width="@dimen/toolbar_app_icon_width" android:height="@dimen/toolbar_app_icon_height" android:drawable="@drawable/ic_app_logo" android:gravity="center"/> </layer-list> 

Y tienes que fijar el ancho y la altura, aunque el vector dibujable ya tiene un tamaño fijo.
Esta es la vista de la barra de herramientas,

 <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/drawable_toolbar_background" android:minHeight="?attr/actionBarSize" android:theme="?attr/actionBarTheme"/> 

Estoy un poco tarde para responder a esto, pero tienen solución maravillosa

  <android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:contentInsetStart="0dp" app:contentInsetEnd="0dp" app:popupTheme="@style/AppTheme.PopupOverlay"> </android.support.v7.widget.Toolbar> <RelativeLayout android:layout_width="match_parent" android:gravity="center" android:layout_height="wrap_content"> <ImageView android:layout_width="wrap_content" android:layout_height="?attr/actionBarSize" android:contentDescription="@string/app_name" android:id="@+id/logoImageView" android:layout_centerInParent="true" android:adjustViewBounds="true" android:src="@drawable/toolbar_background" /> </RelativeLayout> </FrameLayout> </android.support.design.widget.AppBarLayout> 

Y aquí está mi toolbar_background.xml dibujable

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="@color/colorPrimary" /> </shape> </item> <item> <bitmap android:src="@drawable/splash" android:gravity="center" /> </item> 

Salida aquí

  • Cómo cambiar el color de la barra de título sin tema
  • Cómo detectar el evento táctil fuera del cajón de navegación
  • Android: efecto saltar de la barra de estado
  • La barra de herramientas no se desplaza en la barra de estado transparente
  • No solicite Window.FEATURE_ACTION_BAR y defina windowActionBar como false en su tema para usar una barra de herramientas en su lugar
  • Cambio de ActionBarDrawerToggle icon android en la barra de herramientas?
  • Cómo colocar el cajón de navegación debajo de la barra de herramientas?
  • Android Seleccionar texto barra de herramientas opciones color issue
  • Barra de navegación de Android superpuesta a mi vista
  • Obteniendo el "puntero" a NavigationBarView.java por reflexión
  • Cómo ocultar la barra de título desde el principio
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.