Java Code Examples for android.support.design.widget.TabLayout#getChildAt()

The following examples show how to use android.support.design.widget.TabLayout#getChildAt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ViewUtil.java    From MeiZiNews with MIT License 6 votes vote down vote up
/**
 * 动态修改tab的模式
 *
 * @param tabLayout
 */
public static void dynamicSetTablayoutMode(TabLayout tabLayout) {
    int tabTotalWidth = 0;
    for (int i = 0; i < tabLayout.getChildCount(); i++) {
        final View view = tabLayout.getChildAt(i);
        view.measure(0, 0);
        tabTotalWidth += view.getMeasuredWidth();
    }
    if (tabTotalWidth <= MeasureUtil.getScreenSize(tabLayout.getContext()).x) {
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
    } else {
        tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }
}
 
Example 2
Source File: MyUtils.java    From youqu_master with Apache License 2.0 5 votes vote down vote up
private static int calculateTabWidth(TabLayout tabLayout) {
    int tabWidth = 0;
    for (int i = 0; i < tabLayout.getChildCount(); i++) {
        final View view = tabLayout.getChildAt(i);
        view.measure(0, 0); // 通知父view测量,以便于能够保证获取到宽高
        tabWidth += view.getMeasuredWidth();
    }
    return tabWidth;
}
 
Example 3
Source File: LibraryFragment.java    From IslamicLibraryAndroid with GNU General Public License v3.0 5 votes vote down vote up
private void makTabsFixed(@NonNull TabLayout tabLayout) {
    ViewGroup slidingTabStrip = (ViewGroup) tabLayout.getChildAt(0);
    int tabCount = tabLayout.getTabCount();
    for (int i = 0; i < tabCount; i++) {
        View tab = slidingTabStrip.getChildAt(i);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) tab.getLayoutParams();
        layoutParams.weight = 1;
        tab.setLayoutParams(layoutParams);
    }
}
 
Example 4
Source File: MusicUtils.java    From Rey-MusicPlayer with Apache License 2.0 5 votes vote down vote up
public static void changeTabsFont(Context context, TabLayout mTabs) {
    Common common = (Common) context.getApplicationContext();
    ViewGroup vg = (ViewGroup) mTabs.getChildAt(0);
    int tabsCount = vg.getChildCount();
    for (int j = 0; j < tabsCount; j++) {
        ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
        int tabChildsCount = vgTab.getChildCount();
        for (int i = 0; i < tabChildsCount; i++) {
            View tabViewChild = vgTab.getChildAt(i);
            if (tabViewChild instanceof TextView) {
                ((TextView) tabViewChild).setTypeface(TypefaceHelper.getTypeface(context, TypefaceHelper.FUTURA_BOLD));
            }
        }
    }
}
 
Example 5
Source File: TabLayoutUtil.java    From ZZShow with Apache License 2.0 5 votes vote down vote up
private static int calculateTabWidth(TabLayout tabLayout) {
    int tabWidth = 0;
    for (int i = 0; i < tabLayout.getChildCount(); i++) {
        final View view = tabLayout.getChildAt(i);
        view.measure(0,0); //通知父 View 测量,以便于能够保证获取到宽高
        tabWidth += view.getMeasuredWidth();
    }
    return tabWidth;
}
 
Example 6
Source File: TabLayoutActivity.java    From MaterialDesignDemo with MIT License 4 votes vote down vote up
@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_layout);
        mTabLayout = (TabLayout) findViewById(R.id.tabLayout);

        // 手动创建Tab
//        for (int i = 0; i < title.length; i++) {
//            TabLayout.Tab tab = mTabLayout.newTab();
//            tab.setText(title[i]);
////            tab.setIcon(R.mipmap.ic_launcher);//icon会显示在文字上面
//            mTabLayout.addTab(tab);
//        }

        // TabLayout与ViewPager结合使用
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        MyViewPagerAdapter adapter = new MyViewPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(adapter);
//        // 适配器必须重写getPageTitle()方法
//        mTabLayout.setTabsFromPagerAdapter(adapter);
//        // 监听TabLayout的标签选择,当标签选中时ViewPager切换
//        mTabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
//        // 监听ViewPager的页面切换,当页面切换时TabLayout的标签跟着切换
//        mViewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
        // 关联TabLayout与ViewPager,且适配器必须重写getPageTitle()方法
        mTabLayout.setupWithViewPager(mViewPager);


        mLinearLayout = (LinearLayout) mTabLayout.getChildAt(0);
        // 在所有子控件的中间显示分割线(还可能只显示顶部、尾部和不显示分割线)
        mLinearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        // 设置分割线的距离本身(LinearLayout)的内间距
        mLinearLayout.setDividerPadding(50);
        // 设置分割线的样式
        mLinearLayout.setDividerDrawable(ContextCompat.getDrawable(this, R.drawable.divider_vertical));
        mLinearLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));


        // 为TabLayout设置不同状态下的字体大小(并不能成功)
//        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
//            ((TextView) ((LinearLayout) mLinearLayout.getChildAt(i)).getChildAt(1)).setTextSize(10);
////            ((TextView) ((LinearLayout) ((LinearLayout) mTabLayout.getChildAt(0)).getChildAt(i)).getChildAt(0)).setTextSize(12);
//        }
//        ((TextView) ((LinearLayout) mLinearLayout.getChildAt(mTabLayout.getSelectedTabPosition())).getChildAt(1)).setTextSize(30);
//
//        mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//            @Override
//            public void onTabSelected(TabLayout.Tab tab) {
//                ((TextView) ((LinearLayout) mLinearLayout.getChildAt(tab.getPosition())).getChildAt(1)).setTextSize(30);
//            }
//
//            @Override
//            public void onTabUnselected(TabLayout.Tab tab) {
//                ((TextView) ((LinearLayout) mLinearLayout.getChildAt(tab.getPosition())).getChildAt(1)).setTextSize(10);
//            }
//
//            @Override
//            public void onTabReselected(TabLayout.Tab tab) {
//
//            }
//        });


        // 自定义指示器(Indicator)的“长度”的两种方法
        // 方法一:反射
//        setIndicator(mTabLayout,10,10);
        // 方法二:查找子控件
        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, Resources.getSystem().getDisplayMetrics());
        for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
            View tabView = mLinearLayout.getChildAt(0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            tabView.setLayoutParams(params);
        }
    }