【问题标题】:MenuItem renders wrong on 4.0.3MenuItem 在 4.0.3 上呈现错误
【发布时间】:2014-05-22 13:41:57
【问题描述】:

我有一个由 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 android-menu


    【解决方案1】:

    对此可能没有简单的解决方案,这不是 Android 第一次通过更新破坏菜单操作。 例如:Action bar icon size in Android 4.2 我的建议是找到一个适合您在 4.0.3 中需要做的布局,并基于 android 版本,以编程方式调整布局。

    if (Build.VERSION.SDK_INT <= 15)
    {
       //Load alternate Layout
    }
    

    【讨论】:

    • 如果我必须这样做,我会哭的! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    • 2012-01-19
    • 2021-11-25
    • 2013-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多