android 操作栏尺寸高度太大,在移动设备和模拟器中显示不同

android action bar size height is too much larger and showing different in mobile and emulator

我正在开发 android 应用程序,我使用了简单的自定义操作栏。在我的工具栏中,我使用 setLogo() 方法添加了徽标图片,但我的徽标尺寸太大,所以操作栏的高度太大。在我的 android Studio 模拟器中,它显示完美的小尺寸操作栏,但在我的 android 移动设备 phone 中,我的操作栏太大了。

Lolipop 上的模拟器 运行(API 级别 21)和我的 android 手机在 Kitkat 上工作。

但在 android 模拟器中它显示完美

我的代码:

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


    getSupportActionBar().setTitle("  " + currentChatContact.getName());
    getSupportActionBar().setSubtitle("  last seen at 26/05/2015, 05:15 ");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setLogo(R.drawable.boy_thumbnail);

我的工具栏代码是:

<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="@color/defaultPrimaryColor">  </android.support.v7.widget.Toolbar>

我正在使用 AppCompat:

<style name="AppTheme" parent="AppTheme.Base">
    <!-- Customize your theme here. -->
</style>

<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/defaultPrimaryColor</item>
    <item name="android:textColorPrimary">@color/textPrimaryColor</item>
    <item name="android:textColor">@color/secondaryTextColor</item>
    <item name="colorPrimaryDark">@color/darkPrimaryColor</item>
    <item name="colorAccent">@color/accentColor</item>
    <item name="actionOverflowMenuStyle">@style/CMOptionsMenu</item>
</style>

我还没有决定操作栏的任何图像高度,但我在移动设备中得到非常大的图像,如图所示。

请帮我解决这个问题。

对于您的工具栏,删除高度 wrap_content 并像这样放置

android:layout_height="?attr/actionBarSize"

更新

这样试试

Drawable logo=getResources().getDrawable(R.drawable.boy_thumbnail);
        getSupportActionBar().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);
                    }
                }
        }

用这个替换你的工具栏代码

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/primary"
    android:gravity="center_vertical"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/ThemeOverlay.AppCompat.Dark" />