如何改变动作条图标的大小? [英] How to change actionBar icon size?

查看:149
本文介绍了如何改变动作条图标的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在动作条图标谨 图片 输入图像的描述在这里

The actionBar icon should like image

当设备分辨率为1920x1080和android 4.2以上,图标的大小看起来非常小: 图片 输入图像的描述在这里

When the device resolution is 1920x1080 and android 4.2 above, the icon size looks very small: image

(安卓4.1是正确的)

(android 4.1 is correct)

我的问题似乎是<一个href="http://stackoverflow.com/questions/15299508/i-want-to-change-actionbar-icon-size">i-want-to-change-actionbar-icon-size.

图标图像尺寸为113x40,然后我用getActionBar()。getHeight()都会获得动作条的高度为56。

The icon image size is 113x40, then I use getActionBar().getHeight() to get ActionBar height is 56.

menu.xml文件:

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"/>

我尝试下面的方法,但不工作:

I tried following method but doesn't work:

  1. 创造更大的图标图像(ex.170x60)然后放入绘制,华电国际(xhdpi,xxhdpi ...等)的文件夹

  1. create bigger icon image (ex.170x60) then put into drawable-hdpi(xhdpi, xxhdpi...etc.) folder

使用menu.findItem(R.id.menu_buyer).setIcon(resizeImage(INT渣油,INT W,INT h)条);要调整图标

use menu.findItem(R.id.menu_buyer).setIcon(resizeImage(int resId, int w, int h)); to resize icon

添加一个样式&LT;项目名称=机器人:actionBarSize> 200dp&LT;在style.xml /项目> 在清单文件然后设置主题

added a style < item name="android:actionBarSize" > 200dp < /item > in style.xml then set theme in Manifest file

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

Is there a way can show the icon correctly?

推荐答案

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

Too late to answer but, I found the answer

该方法是相当简单。 1,转换成绘制位图,调整它的大小,并重新转换为可绘制并获得菜单项,并设置图标,新的绘制。

the way was quite simple. 1st, convert drawable into bitmap, resize it and re-convert into drawable and get the item of menu and set the icon as the new drawable.

添加您的活动,这些调整大小的方法,

add your Activity these re-size method,

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

and add the work in the 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;
}

这篇关于如何改变动作条图标的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆