美文网首页
TabLayout常常使用到的知识点

TabLayout常常使用到的知识点

作者: KtYY | 来源:发表于2017-06-26 15:46 被阅读0次

    摘要

    本文主要记录TabLayout常常使用到的知识点,避免每次使用TabLayout时,改个小地方,就要花时间去回忆,百度。。。

    目录

    一、TabLayout的下划线属性设置

    二、TabLayout中间的分割线Divider设置

    三、TabLayout的disable设置

    四、TabLayout的左右圆角设置

    五、TabLayout和ViewPager的搭配使用

    正文

    一、TabLayout的下划线属性设置

    Indicator效果图

    TabLayout  tabSelect= (TabLayout) findViewById(R.id.tab_select);

    //add tab

    tabSelect.addTab(tabSelect.newTab().setText("盘亏"));

    tabSelect.addTab(tabSelect.newTab().setText("盘盈"));

    //设置宽度

    TabUtils.setIndicatorHalf(tabSelect);

    工具类:

    public classTabUtils {

    /**

    * 设置TabLayout下方下划线的宽度约为2分1

    *@paramtabs

    */

    public static voidsetIndicatorHalf(finalTabLayout tabs){

              tabs.post(newRunnable() {

                             @Override

                          public voidrun() {

                                    tabs.measure(0,0);

                                    intwidth =tabs.getMeasuredWidth();

                                    setIndicator(tabs,width*1/4,width*1/4);

                            }

              });

    }

    /**

    * 设置TabLayout下方下划线的宽度

    * 至于使用方式的话,直接调用方法即可.必须要在Tablayout渲染出来后调用

    *@paramtabs

    *@paramleftDip

    *@paramrightDip

    */

    public static voidsetIndicator(TabLayout tabs, intleftDip, intrightDip) {

               Class tabLayout = tabs.getClass();

               Field tabStrip =null;

               try{

                       tabStrip = tabLayout.getDeclaredField("mTabStrip");

                }catch(NoSuchFieldException e) {

                       e.printStackTrace();

                }

                tabStrip.setAccessible(true);

                 LinearLayout llTab =null;

               try{

                      llTab = (LinearLayout) tabStrip.get(tabs);

               }catch(IllegalAccessException e) {

                      e.printStackTrace();

              }

              intleft = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());

             intright = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

            // 通过修改Tab的宽度,间接修改Indicator的宽度

              for(int i =0; i < llTab.getChildCount(); i++) {

                     View child = llTab.getChildAt(i);

                     child.setPadding(0,0,0,0);

                     LinearLayout.LayoutParams params =newLinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1);

                     params.leftMargin= left;

                     params.rightMargin= right;

                     child.setLayoutParams(params);

                     child.invalidate();

              }

    }

    }

    二、TabLayout中间的分割线Divider设置

    Divider效果图

    TabLayout  tabSelect= (TabLayout) findViewById(R.id.tab_select); 

    //add tab

    tabSelect.addTab(tabSelect.newTab().setText("盘亏"));

    tabSelect.addTab(tabSelect.newTab().setText("盘盈"));

    //add divider TABs中间分割线

    LinearLayout linearLayout = (LinearLayout)tabSelect.getChildAt(0);    //SlidingTabStrip extends LinearLayout

    //SHOW_DIVIDER_NONE:默认,无分割线, SHOW_DIVIDER_BEGINNING:开始处,

    //SHOW_DIVIDER_MIDDLE:Tabs中间, SHOW_DIVIDER_END:结束处

    linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);    //设置 Tabs 中间分割线显示位置

    linearLayout.setDividerPadding(10);

    linearLayout.setDividerDrawable(ContextCompat.getDrawable(this, R.drawable.style_divider_line));  //设置分割线资源

    三、TabLayout的disable设置

    /**

    * TabSelect不可选时变灰 ,重置

    *@paramenable

    */

    private voidenableTabSelect(booleanenable) {

               LinearLayout linearLayout = ( LinearLayout )tabSelect.getChildAt(0);

               intchildCount = linearLayout.getChildCount();

               for(int i = 0; i < childCount; i++) {

                        View child = linearLayout.getChildAt(i);

                        child.setClickable(enable);

                        if(enable){

                                     child.setBackground(getResources().getDrawable(R.drawable.style_tab_select_bg));

                         }else{

                                      child.setBackgroundColor(getResources().getColor(R.color.all_bg_gary));

                          }

               }

                tabSelect.setSelectedTabIndicatorColor( enable ? getResources().getColor(R.color.khaki): getResources().getColor(R.color.line_color));

                tabSelect.getTabAt(0).select();//重置selected

    }

    四、TabLayout的左右圆角设置

    圆角TabLayout效果展示

    布局文件:

    < android.support.design.widget.TabLayout

    android:layout_centerInParent="true"

    android:id="@+id/tab_select"

    android:layout_width="200dp"

    android:layout_height="wrap_content"

    android:layout_margin="@dimen/padding_four"

    app:tabGravity="fill"

    app:tabIndicatorColor="@color/transparency"

    app:tabMode="fixed"

    app:tabSelectedTextColor="@color/black"

    app:tabTextAppearance="@style/TabLayoutTextStyle"

    app:tabTextColor="@color/white"

    />

    代码:

    TabLayout tabSelect = (TabLayout) findViewById(R.id.tab_select);

    tabSelect.addTab(tabSelect.newTab().setText("RFID模式"));

    tabSelect.addTab(tabSelect.newTab().setText("条码模式"));

    LinearLayout linearLayout = (LinearLayout) tabSelect.getChildAt(0);

    linearLayout.getChildAt(0).setBackgroundResource(R.drawable.style_tab_left_circle);

    linearLayout.getChildAt(linearLayout.getChildCount()-1).setBackgroundResource(R.drawable.style_tab_right_circle);

    drawable文件:

    style_tab_left_circle style_tab_right_circle

    五、TabLayout和ViewPager的搭配使用

    相关文章

      网友评论

          本文标题:TabLayout常常使用到的知识点

          本文链接:https://www.haomeiwen.com/subject/rfcjcxtx.html