Detailed explanation of tablayout usage
Tablayout is a control provided by the design library. It is convenient to use the indicator. Its function is similar to viewpagerindicator. It is very convenient to use. Android studio only needs to introduce it into gradle
Tablayout can be used alone or in combination with viewpager
Let's take a look at the demo used alone to achieve the effect shown in the following figure:
The code is as follows:
Next, let's take a look at the use of viewpager to achieve the following effect diagram:
The layout file is as follows:
The code is as follows:
Tabfragment code is as follows:
extend
1. In addition to the text shown in the above rendering, tablayout can also support drawable and custom view from the source code
The following effect picture is realized by adding a custom view
The code is as follows. You only need to modify the addtab method of tablayout:
2. Replace the default tab selection effect
If you want to use tablayout without its selection effect, for example, we don't want the underline under tab as the indication effect. What should we do?
To replace the default selection effect, you can only add a custom view as a tab. In this way, you can easily handle the selected and unchecked status of the view
At the same time, we have to hide the default underline. How to deal with this? Here is a good suggestion. We can set the color of the slide line to be consistent with the background color of the tab, so we can't see it
For example, the effect we want to achieve below is to enlarge the tab by 1.3 times. If it is not selected, the original scale will be restored. Effect picture:
The modification code is as follows:
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.