Android 对齐工具栏中的图标

Android 对齐工具栏中的图标,android,android-layout,alignment,android-toolbar,Android,Android Layout,Alignment,Android Toolbar,如何将工具栏中的图标与中间对齐?我已经删除了带有getSupportActionBar().setDisplayShowTitleEnabled(false)的标签 为我的工具栏创建一个xml文件是最好的方法吗?是的,在工具栏中创建一些布局,在布局中可以有其他控件(视图) 工具栏和其他布局类似,您可以嵌套它们 <android.support.v7.widget.Toolbar android:id="@+id/toolbar_actionbar" android:

如何将工具栏中的图标与中间对齐?我已经删除了带有
getSupportActionBar().setDisplayShowTitleEnabled(false)的标签

为我的工具栏创建一个xml文件是最好的方法吗?

是的,在工具栏中创建一些布局,在布局中可以有其他控件(视图)

工具栏和其他布局类似,您可以嵌套它们

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

是,在工具栏中创建一些布局,在布局中可以有其他控件(视图)

工具栏和其他布局类似,您可以嵌套它们

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

通过编程,您可以按如下方式执行:

mToolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            ImageView imageView = (ImageView)mToolbar.findViewById(R.id.logo);
            if( mToolbar == null ){
                return;
            }
            int toolbarWidth = mToolbar.getWidth();
            int imageWidth = imageView.getWidth();
            imageView.setX((toolbarWidth-imageWidth)/2);
        }
    });
自定义布局_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>

通过编程,您可以按如下方式执行:

mToolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            ImageView imageView = (ImageView)mToolbar.findViewById(R.id.logo);
            if( mToolbar == null ){
                return;
            }
            int toolbarWidth = mToolbar.getWidth();
            int imageWidth = imageView.getWidth();
            imageView.setX((toolbarWidth-imageWidth)/2);
        }
    });
自定义布局_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>