Super simple lines of code to get the navigation bar function at the bottom of Android

It's super simple. A few lines of code fix the navigation bar at the bottom of Android - C adds two new methods to meet the needs of the project and the messages of small partners:

The implementation of the bottom navigation bar is not difficult, that is, several tabs are switched at the bottom, a FrameLayout is generally at the top, and then the fragment is switched in the FrameLayout.

There are many articles about the navigation bar at the bottom of Android on the Internet, but it seems that they are only about cutting the tab at the bottom. They do not realize the linkage between tab and fragment. They have to write this part of code by themselves when using it. For me, who is lazy (it is said that laziness is a virtue #) of programmers, this is intolerable.

Let's talk about my bottomtabbar.

This is the rendering of the bottomtabbar I encapsulated before. Now let's start with this rendering and analyze it first. You need to set those parameters.

1. Overall background of bottomtabbar

Although we usually use white or light tones close to white as the background, we can't fix it. We should provide such a method so that users can set the background to any color.

2. Picture

Here, you should not only import a picture, but also make some settings:

3. Writing

Similar to the picture, the text also needs to be set:

4. Colour

There are only two colors for text and pictures, one is selected and the other is unselected. We can set them uniformly here and provide a method

5. Margin

Here, you need to set margins in three places:

6. Split line

As mentioned above, the split line needs to be set on the picture. Of course, this is only what some users need to set, so we need to provide a method to set whether to display the split line. In addition, set the height of the split line and its background color

7、fragment

Since the bottomtabbar is linked to the fragment, I must pass in a fragment

These are the general parameters. The following code is shown:

GitHub code connection

The usage is also simple. Just like the title, it can be done in a few lines of code:

Reference method:

1. The first is the XML file code:

Simple, just such a control.

Of course, if you want to set some properties, you need to add namespaces

xmlns:hjm=" http://schemas.android.com/apk/res-auto "

Here is a detailed explanation of the meaning and usage of each parameter:

These parameters can be set in the XML file

2. Activity file code:

It's easy, isn't it.

It is simply mentioned here that the init (getsupportfragmentmanager()) method must be called first. Without this initialization, nothing can be done later.

Maybe you can see that in this init () method, we need to pass in a fragmentmanager, which is under the V4 package. Therefore, we need to pay attention when using activity.

The following is the use of some methods, which have been annotated

It is worth noting that the first four methods must be called before addTabItem (), and if they are called after addTabItem (), there will be no effect.

The correct usage is as follows:

summary

The above is a few lines of super simple code introduced by Xiaobian to you. I hope it can help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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