Use of navigationview in Android and related problem solving

1、 Basic use

1. Add dependencies in the design library of navigationview (the latest is 23.2.0);

2. Add navigationview in the drawerlayout layout;

Note that Android: layout should be set for navigationview_ Gravity = "start" attribute.

3. Then notice that the navigationview is actually divided into two parts, one is the header and the other is the menu list below

As shown in the figure below:

The header is added through the app: headerlayout = "@ layout / nav_header" attribute, NAV_ The layout of the header is as follows:

The menu list below is a menu file, which is added through the app: menu = "@ menu / activity_main_drawer" attribute.

activity_ main_ drawer. The XML file is in the menu folder, with the following contents:

4. Click event of menu list

The click event setting code of the menu list is as follows:

So far, the basic use of navigationview is almost done, and the effect is the effect shown in the previous picture.

The following are the problems and solutions encountered in the process of use.

1、 Menu icon colors are rendered to other colors

Navigationview will render the icons in the menu to the color set by itemicon according to the Android design specification by default. If you do not set this property, it will be rendered to its default dark gray. If you do not want the icon color to be rendered, you can solve it through the following code:

2、 The space between menu icon and text is too large

In the menu of navigationview, the spacing between icon and text is 32dp, but it is usually different from the effect given by our designer. At this time, you can set it by rewriting the following properties:

summary

The above is the whole content of this article. I hope the content of this article can be helpful to Android developers. If you have any questions, you can leave a message.

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
分享
二维码
< <上一篇
下一篇>>