更改为 Android SDK 26 后工具栏中的压扁图标

2022-09-04 01:55:13

将我的应用从编译/定位 SDK v25 更改为 SDK v26 后,我应用工具栏中的所有菜单图标现在都被压扁/挤压/拉伸。

以下是相关的布局代码:

            <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    android:theme="@style/AppTheme.AppBarOverlay" />

                <br.com.mauker.materialsearchview.MaterialSearchView
                    android:id="@+id/search_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

            </RelativeLayout>

        </android.support.design.widget.AppBarLayout>

这是一个挤压的菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item android:id="@+id/action_search"
    android:title="@string/search"
    android:icon="@drawable/ic_search_white_48dp"
    app:showAsAction="ifRoom" />

<item
    android:id="@+id/advanced_search"
    android:enabled="true"
    android:title="@string/advanced_search"
    app:showAsAction="never" />

</menu>

这是另一个挤压的菜单(它们都这样做):

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/add_photo"
    android:enabled="true"
    android:title="@string/add_photo"
    android:icon="@drawable/ic_add_a_photo_white_48dp"
    app:showAsAction="ifRoom" />

</menu>

它在 SDK v25 上的外观

它在 SDK v26 上的外观


答案 1

问题是图标大小比预期的要大。显然,SDK 26中的缩放机制已经发生了变化,现在它会导致这个UI错误。确保提供的工具栏图标资源具有以下大小。

更新:

由于工具栏的最小高度为 和 是 ,因此工具栏图标的最小大小应为 mdpi:abc_action_bar_default_height_material56dpabc_action_bar_icon_vertical_padding_material16dp24dp

drawable-mdpi - 24 x 24 px
drawable-hdpi - 36 x 36 px
drawable-xhdpi - 48 x 48 px
drawable-xxhdpi - 72 x 72 px
drawable-xxxhdpi - 96 x 96 px

答案 2

我在 appcompat-v7 支持库的版本 26.0.0 中遇到了此问题。更新到26.0.2修复了它:)

请参阅此处:https://issuetracker.google.com/issues/64207386


推荐