如何改变actionBar图标大小?

use*_*583 9 android

actionBar图标应该像 图像在此输入图像描述

当设备分辨率为1920x1080和Android 4.2以上时,图标大小看起来非常小: 图像在此输入图像描述

(android 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"/>
Run Code Online (Sandbox Code Playgroud)

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

  1. 创建更大的图标图像(ex.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文件中设置主题

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

Mar*_*oon 9

回答太晚了但是,我找到了答案

方式很简单.1,将drawable转换为位图,调整大小并重新转换为drawable并获取菜单项并将图标设置为新的drawable.

添加你的Activity这些重新调整大小的方法,

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);
}
Run Code Online (Sandbox Code Playgroud)

并在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;
}
Run Code Online (Sandbox Code Playgroud)


Sat*_*jee 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)
Run Code Online (Sandbox Code Playgroud)

您还可以研究此参考链接http://developer.android.com/guide/practices/screens_support.html