2015-02-10 7 views
10

Ich habe versucht, ein Logo in meiner Symbolleiste zu zentrieren. Wenn ich zur nächsten Aktivität navigiere, ist das "upfantance" -Symbol vorhanden, und mein Logo wird leicht nach rechts verschoben. Wie kann ich mein Logo in der Mitte der Symbolleiste beibehalten, ohne das Symbol "Affordanz" zu entfernen?Wie kann ImageView in einer Symbolleiste zentriert werden?

Das ist mein toolbar-Tag ist

<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> 

Antwort

3

In javadoc, heißt es:

Eine oder mehr benutzerdefinierte Ansichten. Die Anwendung kann beliebige untergeordnete Ansichten der Symbolleiste hinzufügen. Sie werden an dieser Position innerhalb des Layouts angezeigt. Wenn die Toolbar.LayoutParams einer untergeordneten Ansicht einen Gravity Wert von CENTER_HORIZONTAL angibt, wird die Ansicht versuchen, den in der Symbolleiste verbleibenden verfügbaren Platz in zu zentrieren, nachdem alle anderen Elemente gemessen wurden.

Es bedeutet, dass Sie es nicht tun, wenn Sie eine benutzerdefinierte toolbar oder entfernen Sie die icon erstellen.

+0

fand ich dieses eine clevere Lösung sein http://stackoverflow.com/a/33082083/1544047 – nmdias

2

Legen Sie das Bild als Hintergrund der Symbolleiste fest, nicht als untergeordnete Ansicht.

<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"> 
+0

Mit LayerList ziehbar http://developer.android.com/guide /topics/resources/drawable-resource.html#LayerList Sie können die Hintergrundfarbe auch einstellen, indem Sie das Logo als Bitmap setzen und einen farbigen Körper darunter legen (oben auf xml). Das ist, wenn es nicht auf dem Symbolleisten-Design festgelegt ist. – Timo

9

Sie können es wie folgt es für mich funktioniert:

<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> 
+2

ziemlich sicher, dass dies nicht funktioniert, wenn Sie der Symbolleiste Menüelemente hinzufügen, da dies die Breite von RelativeLayout verkürzt. – Slickelito

2

Sie können programmatisch tun, das ist der einzige Weg, ich habe es geschafft, ein Logo in der Symbolleiste vollständig zu zentrieren.

Aktivität Sie hinzufügen:

@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); 

} 

und Ihre 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> 
+0

Ich denke, das sollte die akzeptierte Antwort sein, nichts funktioniert aber das! –

0

Ich bin etwas spät, dies zu beantworten, haben aber wunderbare Lösung

<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> 

und hier ist meine toolbar_background.xml, die gezeichnet werden kann

<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> 

output here

1

Für die Devs, die Vektor ziehbar oder SVG-Bilder verwenden.
Dies kann Ihr Hintergrund für Symbolleiste 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> 

und Ja, Sie haben zu reparieren die Breite und Höhe des Elements innerhalb ziehbar, obwohl der Vektor ziehbar bereits sein ist eine feste Größe haben.
Dies ist die Symbolleiste Ansicht,

<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"/> 
+0

Irgendwie funktioniert Ihre Lösung nicht für mich. Das Symbol wird so gestreckt, dass es der Breite der Symbolleiste entspricht. Hast du jemals dieses Verhalten erlebt? Das war das Geschlossene, das ich bisher für Vektor-Drawables gefunden habe ... –

+0

@DominikusK. Ja, ich hatte dieses Problem. Deshalb müssen Sie die Breite und Höhe Ihres Objekts innerhalb von drawable_toolbar_background festlegen. Ich werde es hervorheben. –

Verwandte Themen