2016-07-26 77 views
0

如何將工具欄中的圖標與中間對齊?我已經刪除了標籤getSupportActionBar().setDisplayShowTitleEnabled(false); 爲我的工具欄創建一個xml文件是最好的主意嗎?對齊工具欄中的圖標

回答

1

是在工具欄中創建一些佈局,並在佈局中可以有其他控件(視圖)。

工具欄與其他佈局類似,可以嵌套它們。 。

mToolbar.getViewTreeObserver()addOnGlobalLayoutListener(新ViewTreeObserver.OnGlobalLayoutListener(){ @覆蓋 公共無效onGlobalLayout(){ ImageView的ImageView的=(ImageView的:

<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

編程,你可以如下做到這一點)mToolbar.findViewById(R.id.logo); 如果(mToolbar == NULL){ 回報; } INT toolbarWidth = mToolbar.getWidth(); INT imageWidth = imageView.getWidth(); imageView.setX((toolbarWidth-imageWidth)/ 2); } }); 定製您的layout_toolbar.xml:

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