【问题标题】:How to change actionBar icon size?如何更改 actionBar 图标大小?
【发布时间】:2014-01-26 03:00:53
【问题描述】:

actionBar 图标应该像 image

当设备分辨率为1920x1080,android 4.2以上时,图标尺寸看起来很小: image

(android 4.1 正确)

我的问题好像是i-want-to-change-actionbar-icon-size

图标图片大小为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"/>

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

  1. 创建更大的图标图像(例如 170x60)然后放入 drawable-hdpi(xhdpi, xxhdpi...etc.) 文件夹

  2. 使用 menu.findItem(R.id.menu_buyer).setIcon(resizeImage(int resId, int w, int h));调整图标大小

  3. 在style.xml中添加了一个样式 200dp 然后在 Manifest 文件中设置主题

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

【问题讨论】:

    标签: android


    【解决方案1】:

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

    这很简单。 首先,将drawable转换为位图,调整大小并重新转换为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);
    }
    

    并在 onCreateOptionsMenu 中添加工作

    @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;
    }
    

    【讨论】:

    • 效果很好。谢谢! (安卓 8.1 奥利奥)。但是我建议您store the bitmap locally 避免在每个使用操作栏的活动上调用此方法
    【解决方案2】:

    请保持图像的大小和分辨率。喜欢:

    - 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)
    

    你也可以研究这个参考链接http://developer.android.com/guide/practices/screens_support.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-10
      • 1970-01-01
      • 2014-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多