操作栏中的中心图标

Center icon in Actionbar

我想在 Actionbar 中插入一个居中的图标,但我不知道我需要什么代码。谁能帮帮我?

这是我的代码:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

getSupportActionBar().setIcon(R.mipmap.ic_launcher);
getSupportActionBar().setTitle("");

谢谢

第 1 步:在 R.layout 中创建自定义布局。目录,如你所愿,然后设置视图 到操作栏

第 2 步:设置自定义布局。

   ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowCustomEnabled(true);
    View customView = getLayoutInflater().inflate(R.layout.customActionBar, null);
    actionBar.setCustomView(customView );

从您的 xml 添加如下所示的 ToolBar 并将 Icon 设置为其中的 ImageView。

<android.support.v7.widget.Toolbar
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="?attr/colorPrimary" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@mipmap/ic_launcher"/>

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

按照您的操作将工具栏设置为支持操作栏。