工具栏中的自定义菜单项

时间:2016-05-31 11:35:27

标签: android toolbar menuitem shopping-cart

我正在尝试为项目菜单创建自定义布局。我需要带图像的图标来显示这样的购物车:

image

我创建了一个自定义相对布局:

05-31 05:27:24.519 2353 2462 V AlarmManager: waitForAlarm result :8
05-31 05:27:24.529 1897 1897 I SurfaceFlinger: Device Built-in Screen -> ls 0 
05-31 05:27:24.529 1897 1897 I SurfaceFlinger: Device ScreenRecorder -> ls 0 
05-31 05:27:24.549 1897 1897 I SurfaceFlinger: Device Built-in Screen -> ls 0 
05-31 05:27:24.549 1897 1897 I SurfaceFlinger: Device ScreenRecorder -> ls 0 
05-31 05:27:24.559 1897 1897 I SurfaceFlinger: Device Built-in Screen -> ls 0 
05-31 05:27:24.559 1897 1897 I SurfaceFlinger: Device ScreenRecorder -> ls 0 
05-31 05:27:24.599 1897 1897 I SurfaceFlinger: Device Built-in Screen -> ls 0 
05-31 05:27:24.599 1897 1897 I SurfaceFlinger: Device ScreenRecorder -> ls 0 
05-31 05:27:24.609 1897 1897 I SurfaceFlinger: Device Built-in Screen -> ls 0 
05-31 05:27:24.609 1897 1897 I SurfaceFlinger: Device ScreenRecorder -> ls 0 
05-31 05:27:24.629 1897 1897 I SurfaceFlinger: Device Built-in Screen -> ls 0 
05-31 05:27:24.629 1897 1897 I SurfaceFlinger: Device ScreenRecorder -> ls 0 
05-31 05:27:24.639 4349 4349 D AndroidRuntime: Shutting down VM
05-31 05:27:24.639 4349 4349 W dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x433101a0)
05-31 05:27:24.649 1897 1897 I SurfaceFlinger: Device Built-in Screen -> ls 0 
05-31 05:27:24.649 1897 1897 I SurfaceFlinger: Device ScreenRecorder -> ls 0 
05-31 05:27:24.659 4349 4349 E AndroidRuntime: FATAL EXCEPTION: main
05-31 05:27:24.659 4349 4349 E AndroidRuntime: Process: com.myesporte, PID: 4349
05-31 05:27:24.659 4349 4349 E AndroidRuntime: java.lang.ClassCastException: com.robotium.solo.RobotiumWebClient cannot be cast to org.apache.cordova.engine.SystemWebChromeClient
05-31 05:27:24.659 4349 4349 E AndroidRuntime: at org.apache.cordova.engine.SystemWebView.setWebChromeClient(SystemWebView.java:76)
05-31 05:27:24.659 4349 4349 E AndroidRuntime: at com.robotium.solo.RobotiumWebClient$1.run(RobotiumWebClient.java:59)
05-31 05:27:24.659 4349 4349 E AndroidRuntime: at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java:1747)
05-31 05:27:24.659 4349 4349 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:733)
05-31 05:27:24.659 4349 4349 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
05-31 05:27:24.659 4349 4349 E AndroidRuntime: at android.os.Looper.loop(Looper.java:146)
05-31 05:27:24.659 4349 4349 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5653)
05-31 05:27:24.659 4349 4349 E AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
05-31 05:27:24.659 4349 4349 E AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
05-31 05:27:24.659 4349 4349 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
05-31 05:27:24.659 4349 4349 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
05-31 05:27:24.659 4349 4349 E AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
05-31 05:27:24.679 2353 2782 W ActivityManager: Error in app com.myesporte running instrumentation ComponentInfo{testdroid.appcrawler.testcrawler/testdroid.appcrawler.testcrawler.AppCrawlerRunner}:
05-31 05:27:24.679 2353 2782 W ActivityManager: java.lang.ClassCastException
05-31 05:27:24.679 2353 2782 W ActivityManager: java.lang.ClassCastException: com.robotium.solo.RobotiumWebClient cannot be cast to org.apache.cordova.engine.SystemWebChromeClient
05-31 05:27:24.689 4304 4304 D AndroidRuntime: Shutting down VM
05-31 05:27:24.699 4304 4319 D dalvikvm: GC_CONCURRENT freed 120K, 16% free 758K/900K, paused 0ms+0ms, total 2ms
05-31 05:27:24.819 2353 2462 V AlarmManager: waitForAlarm result :8
05-31 05:27:24.929 2353 2782 I ActivityManager: Killing 4349:com.myesporte/u0a3097 (adj 0): stop com.myesporte
05-31 05:27:24.939 2353 2782 W ActivityManager: Force removing ActivityRecord{2240c9a8 u0 com.myesporte/.MainActivity t18}: app died, no saved state

项目菜单:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="48dp"
    android:layout_height="fill_parent"
    android:layout_gravity="right" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:src="@drawable/ic_shopping_cart"/>

    <TextView
        android:id="@+id/counter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:padding="8dp"
        android:text="5"
        android:textSize="15sp"
        android:textColor="#ffffff" />

</RelativeLayout>

有谁知道怎么做?

提前致谢

修改

MainActivity.java

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/icon_shopping_cart"
        android:actionLayout="@layout/shopping_cart"
        android:title="@string/shopping_icon_description"
        app:showAsAction="always" />
</menu>

3 个答案:

答案 0 :(得分:1)

如果你想在工具栏中使用计数器试试ActionBarMenuItemCounter,它对我有用

  [oldViewControllerObj(self).navigationController pushViewController:newViewControllerObj animated:YES];

答案 1 :(得分:1)

你的设置是正确的,现在只有你需要的东西......

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.your_menu_file, menu);

        final MenuItem item = menu.findItem(R.id.icon_shopping_cart);
        TextView cartCount = (TextView) item.getActionView().findViewById(R.id.counter);
        cartCount.setText("10");
        return true;
    }

删除此行 toolbar.inflateMenu(R.id.shopping_cart);

答案 2 :(得分:0)

我会将您的购物车保存为图像,并按如下方式实现菜单布局:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/icon_shopping_cart"
        android:icon="@drawable/shopping_cart"
        android:title="@string/shopping_icon_description"
        android:showAsAction="always" />
</menu>

并将你的活动/片段膨胀:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_layout, menu);

}