2016-07-26 28 views
0

Wie richte ich mein Symbol in der Symbolleiste an der Mitte aus? Ich habe bereits das Etikett mit getSupportActionBar().setDisplayShowTitleEnabled(false); entfernt. Ist die beste Idee, eine XML-Datei für meine Symbolleiste zu erstellen?Ausrichtungssymbol in der Symbolleiste

Antwort

1

Ja in der Symbolleiste erstellen Sie ein Layout und im Layout können Sie andere Steuerelemente (Ansichten) haben.

Symbolleiste ist wie andere Layouts und Sie können sie verschachtelt werden.

<android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar_actionbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     app:layout_scrollFlags="scroll|enterAlways"> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="?android:attr/actionBarSize"> 
      <TextView 
       android:id="@+id/title" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_centerVertical="true" 
       android:text="@string/news"/> 
     </RelativeLayout> 
    </Toolbar> 
1

programmatisch können Sie es wie folgt:.

mToolbar.getViewTreeObserver() addOnGlobalLayoutListener (neu ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Imageimageview = (Image) mToolbar.findViewById (R.id.logo); if (mToolbar == null) { return; } int toolbarWidth = mToolbar.getWidth(); int imagewidth = imageView.getWidth(); imageView.setX ((toolbarWidth-imageWidth)/2); } }); passen Sie Ihre layout_toolbar.xml an:

<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/toolbar" 
     style="@style/Toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/toolbar_height"                 
     android:background="@drawable/toolbar_background" 
     android:focusable="false"         
     app:titleTextAppearance="@style/AppTheme.Toolbar.Title"> 
     <ImageView 
      android:id="@+id/logo" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/toolbar_logo"/> 

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