Tablayout simple usage summary

This article shares the simple usage of tablayout for your reference. The specific contents are as follows

1、 Tablayout general usage

When using viewpager in projects, it is mostly used in combination with tabpagerindicator, which is a third party and cumbersome to use;

The 2015 Google conference officially released tablayout, which can easily and perfectly achieve this effect;

Because it is officially released, it does not need any third-party things to use; And very simple and clear;

Similarly, if you want to use tablayout, you must configure it in build:

Look at the layout first:

The code is also very simple to use

Step 1: initialize the viewpager and set the adapter step 2: set the title for the tablayout step 3: associate the tablayout with the viewpager

OK, this is the end under normal circumstances, but I encountered a pit when writing this demo: the title can't be displayed alive and dead, which wastes a long time. Finally, after the tablayout is associated with the viewpager, add the title from the new setting:

Generally speaking, tablayout can completely replace tabpagerindicator, and it is relatively simple to use. The most important thing is official;

2、 Tablayout underline width change method:

First of all, Google official does not provide us with a way to change the width of the underline; We can change it through other methods: (two steps) 1. First, define the setindicator () method

2. Add before associating viewpager

Finally, remember to change the sliding mode: app: tabmode = "fixed" the left and right distance can be set according to your own project; (change the underline width based on the first step (tablayout common usage)

3、 Change label alignment

In the XML file: delete app: tabmode = ""; Add app: tabgravity = "center"; You can also limit the label width through app: tabmaxwidth = "150dp" (changing the label alignment needs to be changed based on the first step (tablayout common usage)

4、 Add a dividing line between titles;

1. Create a shape in the drawable folder

2. Add the following code after the main class has set tabrayout:

Click the open link to download the source code for free

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