android - MenuItem 在 4.0.3 上呈现错误

标签 android android-menu

我有一个由 Image 和 textView 组成的 MenuItem:

在 4.3 上显示良好,但在 4.0/4.1 上显示不佳(与 ActionBarCompat 相同的问题):

enter image description here

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/

相关文章:

android - 不将状态栏的颜色更改为 colorPrimary

android - 更改抽屉导航的字体,所以试图找到 textview

android - 自定义 <Key> 的外观

android - 持续部署到 Play Store For Native Application

android - ActionBar 菜单项中的 orderInCategory 是什么以及它为什么用于..?

java - 如何在不破坏和重新创建 fragment 的情况下在 fragment 之间切换? (以类似静态的方式)

android - 改变 ActionBar 的方向

Android 创建下拉菜单

java - 从 Java 到 Python webapp2 的 HTTPPost 多部分(上传文件)

java - 防止 Android 在多任务 View 中显示敏感信息