What should I pass for root when inflating a layout to use for a MenuItem's ActionView?

后端 未结 3 1527
暖寄归人
暖寄归人 2021-02-01 14:23

I have an ImageView that I attach to a MenuItem as its ActionView (the item appears in the ActionBar). The layout for this vi

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-01 14:32

    I would simply do it like this:

    menuItem.setActionView(R.layout.action_view_layout);
    

    Let Android inflate the view for you.

    If you need to do some extra changes on this ImageView call

    ImageView imageView = (ImageView) menuItem.getActionView();
    

    Update

    In order to cater to your curiosity. That is what folks from Google do under the hood:

    public MenuItem setActionView(int resId) {
        final Context context = mMenu.getContext();
        final LayoutInflater inflater = LayoutInflater.from(context);
        setActionView(inflater.inflate(resId, new LinearLayout(context), false));
        return this;
    }
    

提交回复
热议问题