2016-07-26 81 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>