Different tabMode for TabLayout

匿名 (未验证) 提交于 2019-12-03 08:52:47

问题:

I`m using ViewPager and TabLayout. If tabs can be placed on the display tabMode they must be:

app:tabMode="fixed" 

else

app:tabMode="scrollable" 

How can I do this?

回答1:

I din't get your question but i may help you, if tabs count is static or fixed(you know number of tabs) then app:tabMode="fixed" if tabs count is dynamic(data coming from feed) then app:tabMode="scrollable"



回答2:

If the problem is that for certain screen configurations (e.g. small screens, portrait config, etc.) the tabs need to be scrollable because they don't fit the screen and, for other screen config/sizes the number of tabs fit perfectly in the screen, you could just use the resource qualifiers to define your tabs differently for different screen config/sizes.

As an example, imagine your tabs do not fit in portrait but do fit in landscape. Hence one might want the tabs to be scrollable in portrait and fixed in landscape.

res/layout/tabs.xml would contain the TablLayout to use in portrait:

<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.TabLayout xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/tabs"     app:tabMode="scrollable"     android:layout_width="match_parent"     android:layout_height="wrap_content" /> 

and res/layout-land/tabs.xml would contain the TabLayout used in landscape:

<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.TabLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/tabs"     android:layout_width="match_parent"     android:layout_height="wrap_content" /> 

Then in your layout that contains the tabs, just include it like so:

<include layout="@layout/tabs" /> 

Hope this is what you're looking for.



回答3:

In the xml layout, I declared

fixed

and in the fragment java class, I made this:

    if (tabLayout == null) {         tabLayout = (TabLayout) view.findViewById(R.id.tabs);          DisplayMetrics metrics = new DisplayMetrics();         WindowManager wm = (WindowManager) view.getContext().getSystemService(Context.WINDOW_SERVICE);         wm.getDefaultDisplay().getMetrics(metrics);          int width = metrics.widthPixels;         int height = metrics.heightPixels;         double wi = (double) width / (double) metrics.xdpi;         double hi = (double) height / (double) metrics.ydpi;         double x = Math.pow(wi, 2);         double y = Math.pow(hi, 2);         double screenInches = Math.sqrt(x + y);          if (screenInches < 4) {             tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);         }     } 


回答4:

I didn't get your question but answer given in android documentation

when you should use app:tabMode="fixed" or app:tabMode="scrollable"

  1. tabMode="fixed" Fixed tabs display all tabs concurrently and are best used with content that benefits from quick pivots between tabs.

  2. app:tabMode="scrollable" Scrollable tabs display a subset of tabs at any given moment, and can contain longer tab labels and a larger number of tabs. They are best used for browsing contexts in touch interfaces when users don’t need to directly compare the tab labels. This mode is commonly used with a ViewPager.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!