如何更改 actionBar 图标大小?

9

操作栏图标应该像这样: 图片enter image description here

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

(安卓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等)文件夹中

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

  3. 在style.xml中添加样式<item name="android:actionBarSize"> 200dp </item>,然后在Manifest文件中设置主题

有没有一种方法可以正确显示图标?

2个回答

9

虽然有点晚了,但我找到了答案。

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

请按照以下方式在您的活动中添加此重新调整大小的方法。

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

它运行得很好。谢谢!(Android 8.1奥利奥)。然而,我建议您将位图存储在本地,以避免在使用操作栏的每个活动中调用此方法。 - Nuhman

0
请继续保持图片的尺寸和分辨率。例如:
- 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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接