我正在从上一个操作栏迁移到appcompat v21中的新工具栏功能.我仍然希望将徽标保留在操作栏(工具栏)的左上角部分.为了做到这一点,我在我的布局中添加了支持工具栏,我为它创建了一个新的.
- app:theme="@style/NewToolBarStyle"
我正在以编程方式添加日志,因为应用程序中有一些逻辑.
- actionBar.setlogo(R.drawable.myicon);
参考我的新风格(暂时为空):
- <style name="NewToolBarStyle" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
- </style>
然而,结果是显示的图像对于我正在寻找的东西来说太大了,我想知道如何减小图标的大小.
有什么方法(风格,布局或编程)我可以减少徽标大小?
解决方法
材料设计中没有徽标图标:
http://www.google.com/design/spec/layout/structure.html#,所以我认为这不是很好的测试场景 – 或者简单(破碎)的设计.您可以将ImageView添加为工具栏的子窗口小部件,并使用它来显示任何图像.它将显示在所有其他内部窗口小部件的右侧 – 如微调器 – 但列表导航模式也已弃用.
如果您坚持使用徽标,那么我的解决方法是确保工具栏具有固定高度 – 这会处理错误的图标高度.即使在此之后,您还必须在工具栏内部徽标ImageView上将setAdjustViewBounds设置为true – 否则它将创建大的左右填充.
这就是我的工具栏的样子(高度设置为?attr / actionBarSize):
- <?xml version="1.0" encoding="utf-8"?>
- <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_height="?attr/actionBarSize"
- android:layout_width="match_parent"
- android:minHeight="?attr/actionBarSize"
- android:background="?attr/colorPrimary"
- app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
- app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
- </android.support.v7.widget.Toolbar>
使用以下内容在活动布局中引用它:
- <include layout="@layout/toolbar_actionbar"/>
不要在include中更改layout_height.
第二步是在徽标图标上设置AdjustViewBounds(true):
- Drawable logo = getDrawable(iconRes);
- toolbar.setlogo(logo);
- for (int i = 0; i < toolbar.getChildCount(); i++) {
- View child = toolbar.getChildAt(i);
- if (child != null)
- if (child.getClass() == ImageView.class) {
- ImageView iv2 = (ImageView) child;
- if ( iv2.getDrawable() == logo ) {
- iv2.setAdjustViewBounds(true);
- }
- }
- }