Android 如何更改操作栏图标大小?

Android 如何更改操作栏图标大小?,android,Android,actionBar图标应该是 当设备分辨率为1920x1080且安卓4.2以上时,图标大小看起来非常小: (安卓4.1是正确的) 我的问题似乎是 图标图像的大小是113x40,然后我使用getActionBar()。getHeight()来获取ActionBar的高度是56 menu.xml: <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="

actionBar图标应该是

当设备分辨率为1920x1080且安卓4.2以上时,图标大小看起来非常小:

(安卓4.1是正确的)

我的问题似乎是

图标图像的大小是113x40,然后我使用getActionBar()。getHeight()来获取ActionBar的高度是56

menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item 
    android:id="@+id/menu_buyer"
    android:icon="@drawable/buyer_bts"
    android:showAsAction="ifRoom"/>
<item
    android:id="@+id/menu_exhibition_bt"
    android:icon="@drawable/exhibition_bt"
    android:showAsAction="ifRoom"/>
<item
    android:id="@+id/menu_oj"
    android:icon="@drawable/download_bt"
    android:showAsAction="ifRoom"/>

我尝试了以下方法,但不起作用:

  • 创建更大的图标图像(例如170x60),然后放入可绘制hdpi(xhdpi、xxhdpi…等)文件夹

  • 使用menu.findItem(R.id.menu_buyer).setIcon(resizeImage(intresid,intw,inth));调整图标大小的步骤

  • 在style.xml中添加了样式200dp 然后在清单文件中设置主题


  • 有没有办法正确显示图标?

    请保持图像的大小和分辨率。比如:

    - drawable-ldpi (it need resolution or ppi is ~120)(keep small size image)
     - drawable-mdpi (it need resolution or ppi is ~160)(keep normal size image)
     - drawable-hdpi (it need resolution or ppi is ~240)(keep larger size image)
     - drawable-xhdpi (it need resolution or ppi is ~320)(keep xlarger size image)
    

    你也可以研究这个参考链接

    来不及回答,但我找到了答案

    这很简单。 首先,将drawable转换为位图,调整其大小并重新转换为drawable。然后获取菜单项并将图标设置为新的可绘制图标

    请在您的活动中添加此重新调整大小方法,如下所示

    private Bitmap resizeBitmapImageFn(
            Bitmap bmpSource, int maxResolution){
        int iWidth = bmpSource.getWidth();
        int iHeight = bmpSource.getHeight();
        int newWidth = iWidth ;
        int newHeight = iHeight ;
        float rate = 0.0f;
    
        if(iWidth > iHeight ){
            if(maxResolution < iWidth ){
                rate = maxResolution / (float) iWidth ;
                newHeight = (int) (iHeight * rate);
                newWidth = maxResolution;
            }
        }else{
            if(maxResolution < iHeight ){
                rate = maxResolution / (float) iHeight ;
                newWidth = (int) (iWidth * rate);
                newHeight = maxResolution;
            }
        }
    
        return Bitmap.createScaledBitmap(
                bmpSource, newWidth, newHeight, true);
    }
    
    它工作得很好。谢谢(安卓8.1奥利奥)。但是,我建议您避免在使用actionbar的每个活动上调用此方法
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
    
        Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_navigation_drawer); //Converting drawable into bitmap
        Bitmap new_icon = resizeBitmapImageFn(icon, 50); //resizing the bitmap
        Drawable d = new BitmapDrawable(getResources(),new_icon); //Converting bitmap into drawable
        menu.getItem(0).setIcon(d); //choose the item number you want to set
        return true;
    }