在代码中更改Actionbar溢出图标:奇怪的大小调整

时间:2013-07-19 10:14:31

标签: java android svg icons android-actionbar

目标

将操作栏的溢出项目图标更改为其他颜色。 新图标应通过svg。

输入

国家

请注意,溢出图标将替换为刷新图标以检查大小差异。稍后将替换为溢出图标。这两个图标都使用same .svg file中使用相同参数的相同方法加载 我使用svg-android-2进行.svg加载和渲染。 Current state

修改代码

在解决问题之前,请不要讨论使用这种反射代码的危险:)

// http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.2_r1/com/android/internal/app/ActionBarImpl.java/
private void hackOverflowIcon() {
    ActionBar actionBar = getActionBar();

    if (actionBar.getClass().getName().equals("com.android.internal.app.ActionBarImpl")) {
        try {
            Field actionBarViewField = actionBar.getClass().getDeclaredField("mActionView");
            actionBarViewField.setAccessible(true);
            Object actionBarView = actionBarViewField.get(actionBar);

            Field actionMenuViewField = actionBarView.getClass().getSuperclass().getDeclaredField("mActionMenuPresenter");
            actionMenuViewField.setAccessible(true);
            Object actionMenu = actionMenuViewField.get(actionBarView);

            Field overFlowViewField = actionMenu.getClass().getDeclaredField("mOverflowButton");
            overFlowViewField.setAccessible(true);
            ImageButton overFlowView = (ImageButton) overFlowViewField.get(actionMenu);

            Drawable drawable=SVGUtils.getDrawableFromRaw(this, R.drawable.refresh, new RectF(0, 0, 120, 120));
            overFlowView.setImageDrawable(drawable);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    } else
        throw new IllegalStateException("Actionbar is an instance of " + actionBar.getClass().getName());
}

问题

为什么图标尺寸不同,如何防止尺寸变化?

感谢阅读和有关此问题的任何帮助,
直到

0 个答案:

没有答案