android – AppCompat v21工具栏更改徽标大小

前端之家收集整理的这篇文章主要介绍了android – AppCompat v21工具栏更改徽标大小前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在从上一个操作栏迁移到appcompat v21中的新工具栏功能.我仍然希望将徽标保留在操作栏(工具栏)的左上角部分.为了做到这一点,我在我的布局中添加支持工具栏,我为它创建了一个新的.
  1. app:theme="@style/NewToolBarStyle"

我正在以编程方式添加日志,因为应用程序中有一些逻辑.

  1. actionBar.setlogo(R.drawable.myicon);

参考我的新风格(暂时为空):

  1. <style name="NewToolBarStyle" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
  2. </style>

然而,结果是显示的图像对于我正在寻找的东西来说太大了,我想知道如何减小图标的大小.

有什么方法(风格,布局或编程)我可以减少徽标大小?

解决方法

材料设计中没有徽标图标: http://www.google.com/design/spec/layout/structure.html#,所以我认为这不是很好的测试场景 – 或者简单(破碎)的设计.您可以将ImageView添加为工具栏的子窗口小部件,并使用它来显示任何图像.它将显示在所有其他内部窗口小部件的右侧 – 如微调器 – 但列表导航模式也已弃用.

如果您坚持使用徽标,那么我的解决方法是确保工具栏具有固定高度 – 这会处理错误的图标高度.即使在此之后,您还必须在工具栏内部徽标ImageView上将setAdjustViewBounds设置为true – 否则它将创建大的左右填充.

这就是我的工具栏的样子(高度设置为?attr / actionBarSize):

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.v7.widget.Toolbar
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. android:id="@+id/toolbar"
  6. android:layout_height="?attr/actionBarSize"
  7. android:layout_width="match_parent"
  8. android:minHeight="?attr/actionBarSize"
  9. android:background="?attr/colorPrimary"
  10. app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  11. app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
  12. </android.support.v7.widget.Toolbar>

使用以下内容在活动布局中引用它:

  1. <include layout="@layout/toolbar_actionbar"/>

不要在include中更改layout_height.

第二步是在徽标图标上设置AdjustViewBounds(true):

  1. Drawable logo = getDrawable(iconRes);
  2. toolbar.setlogo(logo);
  3. for (int i = 0; i < toolbar.getChildCount(); i++) {
  4. View child = toolbar.getChildAt(i);
  5. if (child != null)
  6. if (child.getClass() == ImageView.class) {
  7. ImageView iv2 = (ImageView) child;
  8. if ( iv2.getDrawable() == logo ) {
  9. iv2.setAdjustViewBounds(true);
  10. }
  11. }
  12. }

猜你在找的Android相关文章