Is there a way to add margin between the tabs in a TabLayout? I've tried with using a custom style for Widget.Design.TabLayout, but there are properties only related to padding, but no margins.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
@Todor Kostov answered well, but the center of the tabs are slipped away because the last tab has margin too.
so use
mTabLayout.getTabCount() - 1
instead of justmTabLayout.getCodeCount()
.Here's how I did it in pure xml.
dimens.xml:
Layout containing your TabLayout:
Add this to your theme in styles.xml:
drawable-nodpi\tab_background.xml:
drawable-nodpi\tab_background_selected.xml:
...That's where the trick is. Effectively, wrapping your background shape in an
item
with "padding" according to your@dimen/tab_spacing_half
value. And finally...drawable-nodpi\tab_background_unselected.xml:
Ok mates, after spending 2-3 hours on that I finally found a solution.
If you are using TabLayout there is no way to add margins to the tabs by using styles and so on. (as @Connecting life with Android earlier)
But, you can do that by writing some Java code. All in all your code should look similar to that one:
In order to get each and every tab as a View we have to first get the container which contains them. In this case the TabLayout is using a SlidingTabStrip as a container for the tabs. The SlidingTabStrip is the first child of the TabLayout:
And after this small detail, everything is pretty straight forward.
This is how set margin for four different tabs. You can change the setMargins(v1,v2,v3,v4) function values to get a suitable fitting for the number of tabs that you are working with. I hope this helps. Please note that tabHost is the object of TabHost hosting different tabs you are working with.