工具栏图像居中

使用最后一个android.support.v7.widget.Toolbar我想在工具栏中居中一个图像,但它保持在左侧。

对我来说最好的方法是使用toolbar.setLogo()方法,并将徽标居中,但我没有看到任何方式来做到这一点。

我使用Toolbar的布局:

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

有没有可能将图像(徽标)添加到工具栏并居中? 编程或在布局?

工具栏只是一个ViewGroup,你可以自定义你想要的。

尝试这个 :

 <android.support.v7.widget.Toolbar style="@style/ToolBarStyle" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:minHeight="@dimen/abc_action_bar_default_height_material"> <ImageView android:layout_width="wrap_content" android:contentDescription="@string/logo" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/ic_launcher"/> </android.support.v7.widget.Toolbar> 

这应该把你的imageView在工具栏的中心。

如果您有任何意见,并且无法将徽标居中,那将是因为居中基于剩余空间,而不是整个工具栏。

为了保证徽标居中,请尝试使用图层列表作为工具栏的背景,而不是尝试将图像用作视图。

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" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@drawable/toolbar_background" android:theme="@style/ThemeOverlay.AppCompat.Dark" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:layout_scrollFlags="scroll|enterAlways" app:layout_collapseMode="pin"> </android.support.v7.widget.Toolbar> 

toolbar_background.xml

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="@color/colorPrimary" /> </shape> </item> <item> <bitmap android:src="@drawable/logo" android:gravity="center" /> </item> </layer-list> 

这应该确保一个中心的标志,具有特定的背景颜色。

这不是最好的解决scheme,但即使您有button或菜单项,也可以解决这个问题。

尝试

 <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar style="@style/ToolBarStyle" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:minHeight="@dimen/abc_action_bar_default_height_material"/> <ImageView android:layout_width="wrap_content" android:contentDescription="@string/logo" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/ic_launcher"/> </FrameLayout> 

我有一个类似的问题。 我尝试使用蓝莓build议的层次列表方法,但是一旦我为标识添加了高分辨率资源,该解决scheme就不再有效。 层列表中的位图有一个错误:它会放大较小的图像以适应空间,但不会缩小较大的图像。 所以,只要你可以使用细小的图像,当它们放大时看起来像颗粒状/像素化,层列表就可以正常工作。

为了解决这个问题,并有一个居中的高分辨率的标志,我从工具栏中完全删除了android:background属性,将android:background="?attr/colorPrimary"到我的AppBarLayout父项中,然后将这个可怕的代码扔进我的Activity的onCreate方法:

  final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); ActionBar ab = getSupportActionBar(); if (ab != null && toolbar != null) { // Hide the title text. ab.setDisplayShowTitleEnabled(false); // Convert and show the logo. toolbar.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { @Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { ActionBar ab = getSupportActionBar(); if (ab != null) { Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.logo); Bitmap bmp2 = Bitmap.createBitmap(toolbar.getWidth(), toolbar.getHeight(), bmp.getConfig()); Bitmap bmp3 = Bitmap.createScaledBitmap(bmp, bmp.getWidth() * toolbar.getHeight() / bmp.getHeight(), toolbar.getHeight(), true); Canvas canvas = new Canvas(bmp2); canvas.drawBitmap(bmp3, (toolbar.getWidth() / 2) - (bmp3.getWidth() / 2), 0, null); BitmapDrawable background = new BitmapDrawable(getResources(), bmp2); ab.setBackgroundDrawable(background); } } }); } 

它的工作原理是创build一个工具栏大小的canvas和比例缩放的徽标版本,将新resize的徽标绘制到中间的canvas上,最后将正在绘制的位图设置为操作栏的背景。

另一方面,这种方法可以让你完全控制位置,大小等等。不限于你在XML中可以做的任何事情。 这太糟糕了,因为每当工具栏布局发生变化时都会定期调用它,这意味着性能会受到影响。 但是,当你的devise团队(不关心材料devise的第一件事)需要你有一个背景标志,并浪费五个小时寻找上述解决scheme时,你的performance就会停止 – 你只是想让该死的东西起作用。 上面的代码令人绝望地发狂。

我会让Android开发人员比我更有能力指出“我做错了”,并解决上述代码带来的性能问题/错误。 它不会改变层列表位图被破坏的事实,这是一个破解(这不应该存在),以修复Android中的一个愚蠢的错误(也应该不存在)。

如果你需要在工具栏中放置一个更复杂的布局 ,而不仅仅是一个图像,这是我放在我的Activity的onCreate()中的

 mToolbar.addOnLayoutChangeListener( new View.OnLayoutChangeListener() { @Override public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) { // Push layout to the center int toolbarCenterX = (int) (mToolbar.getWidth() / 2.0 - mMiddleToolbarLayout.getWidth() / 2.0)); if(mMiddleToolbarLayout.getX() != toolbarCenterX) { mMiddleToolbarLayout.setX(toolbarCenterX); } } } ); 

onLayoutChange被调用时,你的布局的宽度已经确定,所以你只要把它推到中心。

注意:仅当居中工具栏居中时,才将工具栏居中(请参阅if条件),否则此callback将在无限循环中重新调用。

XML文件将如下所示:

 <android.support.v7.widget.Toolbar ... > <LinearLayout android:id="@+id/middle_toolbar_layout" ... > </android.support.v7.widget.Toolbar>