getActionView() of my MenuItem return null

后端 未结 5 1257
囚心锁ツ
囚心锁ツ 2020-11-30 13:41

I just would like to tweak the View of an ActionBar MenuItem by code.

Unfortunately, it seems that getActionView always return null!

My code:



        
相关标签:
5条回答
  • 2020-11-30 13:55

    getActionView() only works if there's a custom actionView from setActionView.

    0 讨论(0)
  • 2020-11-30 14:03

    you should use

    app:actionLayout="@layout/menu_actionbar_basket"
    

    thats the trick if you use

    android:actionLayout="@layout/menu_actionbar_basket"
    

    you would always get null exception in default toolbar.

    0 讨论(0)
  • 2020-11-30 14:06

    If your debug build is working without any issues and issue is only with release build then this may be because of proguard configuration. If you have enabled proguard in your application then add below line to proguard-rules.pro

    -keep class android.support.v7.widget.SearchView { *; }
    
    0 讨论(0)
  • 2020-11-30 14:09

    For me the solution that worked is to use app namespace instead of android.

    app:actionViewClass="android.support.v7.widget.SearchView"

    Don't forget to declare it: xmlns:app="http://schemas.android.com/apk/res-auto"

    0 讨论(0)
  • 2020-11-30 14:13

    First Solution

    This happen may be you not set actionLayout in Menu file so set your actionLayout in menu file

    app:actionLayout="@layout/your_action_layout"

    Second Solution and the second solution is

    from

    android:actionLayout="@layout/your_action_layout"

    to

    app:actionLayout="@layout/your_action_layout"

    0 讨论(0)
提交回复
热议问题