Android 4.2中的操作栏图标大小是否已更改? 我有一个120x48px HDPI图标,在Android 4.1及更低版本中完美呈现。它仍然是。
然而,在任何4.2设备上,它被压缩为适合我所看到的48x48px。或类似的东西;这绝对是一个广场。
有什么想法吗?谢谢!
答案 0 :(得分:5)
这并不理想,但您可以使用自定义操作视图来解决此限制。
这样的事情:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/log_in"
android:title="Login"
android:showAsAction="always"
android:actionLayout="@layout/log_in_button"/>
</menu>
其中@ layout / log_in_button指向包含ImageButton的布局文件,并将您的图标设置为src。
您必须在OnCreateOptionsMenu方法中绑定单击侦听器。这里有一个很好的例子:http://developer.android.com/guide/topics/ui/actionbar.html#ActionView。
我只是学会了使用这种方法,所以除了增加的复杂性之外我还不知道是否有任何重大的挫折。
答案 1 :(得分:2)
所以,我找到了一个答案,它有点像hacky但是有效(TM):
一般的想法是监听布局更改并将新边界应用于drawable。这看起来像这样:
public static void updateActionBar(final Activity activity) {
if (Build.VERSION.SDK_INT >= 17) {
try {
final View content = activity.findViewById(android.R.id.content);
if (content instanceof FrameLayout) {
final FrameLayout contentFrameLayout = (FrameLayout) content;
final ViewParent parent = contentFrameLayout.getParent();
if (parent instanceof LinearLayout) {
final LinearLayout parentLinearLayout = (LinearLayout) parent;
final Class<?> actionBarContainerClass = Class.forName("com.android.internal.widget.ActionBarContainer");
final Class<?> actionBarViewClass = Class.forName("com.android.internal.widget.ActionBarView");
final Class<?> actionMenuViewClass = Class.forName("com.android.internal.view.menu.ActionMenuView");
final Class<?> actionMenuItemViewClass = Class.forName("com.android.internal.view.menu.ActionMenuItemView");
for (int i = 0, childCount = parentLinearLayout.getChildCount(); i < childCount; i++) {
final View parentLinearLayoutChild = parentLinearLayout.getChildAt(i);
handleParentLinearLayoutChild(actionBarContainerClass, actionBarViewClass, actionMenuViewClass, actionMenuItemViewClass, parentLinearLayoutChild);
}
}
}
} catch (Exception e) {
// Handle or ignore
}
}
}
private static void handleParentLinearLayoutChild(final Class<?> actionBarContainerClass, final Class<?> actionBarViewClass, final Class<?> actionMenuViewClass, final Class<?> actionMenuItemViewClass, final View parentLinearLayoutChild) {
if (parentLinearLayoutChild instanceof FrameLayout && parentLinearLayoutChild.getClass().equals(actionBarContainerClass)) {
final FrameLayout actionBarContainer = (FrameLayout) parentLinearLayoutChild;
for (int i = 0, actionBarContainerChildCount = actionBarContainer.getChildCount(); i < actionBarContainerChildCount; i++) {
final View actionBarContainerChild = actionBarContainer.getChildAt(i);
handleActionBarContainerChild(actionBarViewClass, actionMenuViewClass, actionMenuItemViewClass, actionBarContainerChild);
}
}
}
private static void handleActionBarContainerChild(final Class<?> actionBarViewClass, final Class<?> actionMenuViewClass, final Class<?> actionMenuItemViewClass, final View actionBarContainerChild) {
if (actionBarContainerChild instanceof ViewGroup && actionBarContainerChild.getClass().equals(actionBarViewClass)) {
final ViewGroup actionBarView = (ViewGroup) actionBarContainerChild;
actionBarView.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
@Override
public void onChildViewAdded(final View parent, final View child) {
handleActionBarViewChild(child, actionMenuViewClass, actionMenuItemViewClass);
}
@Override
public void onChildViewRemoved(final View parent, final View child) {
}
});
for (int i = 0, actionBarViewCount = actionBarView.getChildCount(); i < actionBarViewCount; i++) {
handleActionBarViewChild(actionBarView.getChildAt(i3), actionMenuViewClass, actionMenuItemViewClass);
}
}
}
private static void handleActionBarViewChild(final View child, final Class<?> actionMenuViewClass, final Class<?> actionMenuItemViewClass) {
try {
if (child instanceof LinearLayout && child.getClass().equals(actionMenuViewClass)) {
final LinearLayout actionMenuView = (LinearLayout) child;
actionMenuView.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
@Override
public void onChildViewAdded(final View parent, final View child) {
handleActionMenuViewChild(child, actionMenuItemViewClass);
}
@Override
public void onChildViewRemoved(final View parent, final View child) {
}
});
for (int i = 0, actionMenuViewCount = actionMenuView.getChildCount(); i < actionMenuViewCount; i++) {
handleActionMenuViewChild(actionMenuView.getChildAt(i), actionMenuItemViewClass);
}
}
} catch (Exception e) {
// Handle or ignore
}
}
private static void handleActionMenuViewChild(final View child, final Class<?> actionMenuItemViewClass) {
try {
if (child instanceof TextView && child.getClass().equals(actionMenuItemViewClass)) {
final TextView menuViewChild = (TextView) child;
final Drawable[] compoundDrawables = menuViewChild.getCompoundDrawables();
final Drawable leftDrawable = compoundDrawables[0];
final int intrinsicWidth = leftDrawable.getIntrinsicWidth();
final int intrinsicHeight = leftDrawable.getIntrinsicHeight();
leftDrawable.setBounds(0, 0, intrinsicWidth , intrinsicHeight );
menuViewChild.setCompoundDrawables(leftDrawable, null, null, null);
menuViewChild.setPadding(menuViewChild.getPaddingLeft(), 0, menuViewChild.getPaddingRight(), 0);
menuViewChild.invalidate();
menuViewChild.requestLayout();
}
} catch (Exception e) {
// Handle or ignore
}
}
然后,您必须在以下回调中的每个Activity中调用updateActionBar(我建议您进行扩展的抽象基本活动): 的onCreate onMenuOpened(我发现如果你把它叫做延迟(例如200ms),它会提高性能并减少闪烁(drawables的大小变化)) onPrepareOptionsMenu(我发现如果你把它叫做延迟(例如200ms),它会提高性能并减少闪烁(drawables的大小变化)
这适用于Nexus 7和Android 4.2的Nexus 10。您可以预期它会因未来的更新而失败,但至少目前它似乎有效。