English 中文(简体)
如何获得和充实通过习俗布局产生的菜单?
原标题:How to get and populate a menu item created through custom layout?

我曾试图把图像装入一个菜单上的项目,这是通过习俗布局制作的,并且一直在检查正式文件和员额,例如,Mendu 项目的保管观点。

参看我关于西班牙的论文。

  @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        final MenuItem alertMenuItem = menu.findItem(R.id.action_my_personal);
        FrameLayout rootView = (FrameLayout) alertMenuItem.getActionView();

        redCircle = (FrameLayout) rootView.findViewById(R.id.view_alert_red_circle);
        countTextView = (TextView) rootView.findViewById(R.id.view_alert_count_textview);
        ImageView hc_image_menu_inflated = (ImageView) rootView.findViewById(R.id.hc_image_menu);

        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onOptionsItemSelected(alertMenuItem);
            }
        });


        if (menu.findItem(R.id.hc_image_menu) != null) {
            loadMenuIcon(menu.findItem(R.id.hc_image_menu), my_image_url);
        }

        return super.onPrepareOptionsMenu(menu);
    }

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.toolbar_menu_main, menu);

        return true;
    }

As you can see I have added these lines to avoid the app crashing, as menu.finditem(R.id.hc_image_menu) is returning as null and that s where the problem is, as I want to populate the ImageView that has the id hc_image_menu but am not being successful despite so many attempts

 if (menu.findItem(R.id.hc_image_menu) != null) {
        loadMenuIcon(menu.findItem(R.id.hc_image_menu), my_image_url);
    }

我的toolbar_main_menu布稿是:

<item
        android:id="@+id/action_my_personal"
        app:actionLayout="@layout/toolbar_menu_hc"
        android:orderInCategory="100"
        android:title="My Personal Trainer"
        app:showAsAction="always" />

对于我的习惯布局(toolbar_hc_menu)来说,这是:

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="48dp"
android:layout_height="32dp"
android:layout_gravity="center">


<ImageView
    android:id="@+id/hc_image_menu"
    android:layout_marginRight="8dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:src="@drawable/gavin"/>

<FrameLayout
    android:id="@+id/view_alert_red_circle"
    android:layout_width="14dp"
    android:layout_height="14dp"
    android:layout_gravity="top|end"
    android:background="@drawable/date_circle"
    android:visibility="gone"
    tools:visibility="visible">

    <TextView
        android:id="@+id/view_alert_count_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="@color/white"
        android:textSize="10sp"
        tools:text="3"/>

</FrameLayout>

非常感谢这方面的任何帮助。 确实非常赞赏!

最佳回答

议题:

I had to load the image into the ImageView after getting the alertMenuItem.

if (my_image_url!= null) {
        Picasso.with(getApplicationContext()).load(my_image_url).into(hc_image_menu_inflated);
    }
问题回答

暂无回答




相关问题
Very basic WPF layout question

How do I get this listbox to be maxiumum size, i.e. fill the group box its in. I ve tried width="auto" Height="auto", width="stretch" Height="stretch" annoyingly at the moment, it dynamicly sizes to ...

How to align the text to top of TextView?

How to align the text to top of a TextView? Equivalent Android API for Swings setInsets()? that is top of text should start be in (0,0) of TextView <?xml version="1.0" encoding="utf-8"?> <...

Centring a flow in Shoes

Can I center the contents of a flow in Shoes? I know that a paragraph can be centred like: para Centred paragrpah , :align=> center However, this does not work with flows: flow(:align=> ...

Overlaying with CSS

I want to load a website in an iframe, and overlay the website with some hints about the website that is shown. However, i got stuck at the point that the website has a variable passive white space at ...

How do you normally make a program look beautiful? [closed]

How can I make an Application look nice and not like an amateur pulled it together? I mean graphic-wise. Is there some sort of book you can read regarding beautiful program layouts, etc? I put this ...

热门标签