Android custom actionprovider toolbar implements menu red dots

Today's goals: 1 Custom actionprovider 2 Toolbar actionbar custom menu 3 Add a corner sign to the menu on the right side of the toolbar actionbar (add a small red dot to the toolbar actionbar menu). The source code is at the end of the article.

--------------------------------------------------------------------------------

Effect preview

Customizing the menu does not affect any effect of the native MD. Attributes such as text and color can be controlled externally.

Requirements description and analysis

We usually customize the titlebar, which can achieve many customization effects. However, after the toolbar came out, it was really easy to use, so we all used the toolbar, but what should we do to achieve an effect similar to the badgeview (such as the small red dot in the figure above) for the menu button on the right?

After seeing this request, students who have not played actionprovider may be flustered, but please first analyze the composition of this menu with me: an ImageView displays icons, a textview displays numbers, and a circular background (which can be any shape or color). Then you can't use the system's menu XML, so to customize the menu, some people may think of customizing the view, but they don't need it.

We usually write menu in menu XML is as follows:

In fact, the item of this menu has an Android: actionproviderclass attribute, which can be used to customize the content (view) displayed here. The value of this attribute is a complete class name, such as com yanzhenjie. Xxooprovider, and this xxooprovider needs to inherit actionprovider, and all specific operations are completed in actionprovider.

Inherit actionprovider first

Special instructions are needed here. Because the toolbar is under the support package, we need to use the actionprovider class under support. This class is in support In V4, it is compatible with the toolbar and actionbar, so we can customize this actionprovider class and use it freely in the toolbar and actionbar.

Because we want to implement the corner marker, let's call this class badgeactionprovider for the time being. Inheriting actionprovider needs to implement oncreateactionview() method:

Custom layout: badgeview to display

We see that the oncreateactionview () class needs to return a view, which is the view we want to customize, so let's next define the displayed view, which is simply an ImageView and a textview analyzed at the beginning of the article:

Only Android: background = "? Actionbaritembackground" needs to be explained in the above code is the click effect of the menu referencing the system default toolbar / actionbar. Textview's Android: background = "@ drawable / circle_red" is the round red background.

Combination of actionprovider and custom layout

The above inheritance has been completed, and the custom layout has been written. The next step is the combination of the two:

The only doubt about the above code:

int size = getContext(). getResources(). getDimensionPixelSize( android.support.design.R.dimen.abc_action_bar_default_height_material);

This sentence reads the height of the toolbar / action bar under support to make the height and width of the menu consistent with the system menu.

OK, now the customization of actionprovider is basically over. At most, add some custom methods:

How to use a custom actionprovider

Now go back to the beginning of the article and fill in the full custom actionprovider class name for the Android: actionproviderclass attribute. As mentioned earlier, this is the suprt class, so here we need to change a little:

We notice that this becomes app: actionproviderclass because the XML header introduces the xmlnamespace.

Java code control custom menu

Above menu After the XML preparation is completed, the next step is to load the XML into our activity or fragment with java code. Here, we take activity as an example and inherit appcompatactivity.

First, when loading the menu, record the actionprovider we just customized:

Special note: many students have null pointer exceptions when setonclicklistener here. The reason is that the custom class name referenced in the code app: actionproviderclass = "com. Yanzhenjie. Badgeactionprovider" is wrong. You should fill in your package name instead of copying the one in the blog.

Setting click listening here was also emphasized above. Here is a pit to talk about. We just provided several methods to set icon and text. We can't call it directly in oncreateoptionsmenu(). Because actionprovider hasn't finished loading and initialization, if we dynamically initialize icon and text, we need to set them in onwindowfocuschanged():

It should also be explained here that oncreateoptionsmenu() cannot be initialized directly, and can be called at will after oncreateoptionsmenu() method is executed.

So now you can set the displayed text and icons at will, and all the goals mentioned at the beginning of our article have been completed,

Source code download: http://xiazai.jb51.net/201609/yuanma/AndroidActionProvider (jb51.net). rar

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>