我有一个由 Image 和 textView 组成的 MenuItem:
在 4.3 上显示良好,但在 4.0/4.1 上显示不佳(与 ActionBarCompat 相同的问题):
menu_action_cart.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/actionCart"
android:actionLayout="@layout/menu_cart_button"
android:title="@string/action_cart"
android:showAsAction="always"/>
</menu>
menu_cart_button.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="48dp"
android:layout_height="fill_parent"
android:layout_margin="10dp">
<ImageView
android:id="@+id/actionIcon"
android:layout_width="48dp"
android:layout_height="fill_parent"
android:clickable="true"
android:src="@drawable/menu_bag" />
<TextView
android:id="@+id/actionbar_notification_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:paddingTop="10dp"
android:textColor="@color/red"/>
</RelativeLayout>
Java:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.menu_action_cart, menu);
MenuItem menuItem = menu.findItem(R.id.actionCart);
View badgeLayout = menuItem.getActionView();
TextView textView = (TextView) badgeLayout.findViewById(R.id.actionbar_notification_text);
ImageView imageView = (ImageView) badgeLayout.findViewById(R.id.actionIcon);
textView.setText("1");
imageView.setEnabled(true);
textView.setTextColor(getResources().getColor(R.color.orogo_red));
imageView.setImageResource(R.drawable.menu_bag);
return super.onCreateOptionsMenu(menu);
}
最佳答案
对此可能没有简单的解决方案,这不是 Android 第一次通过更新破坏菜单操作。 例如:Action bar icon size in Android 4.2 我的建议是找到适合您在 4.0.3 中需要执行的操作的布局,并基于 android 版本,以编程方式调整布局。
if (Build.VERSION.SDK_INT <= 15)
{
//Load alternate Layout
}
关于android - MenuItem 在 4.0.3 上呈现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23808634/