【问题标题】:How to change the text color of SlidingTabLayout?如何更改 SlidingTabLayout 的文字颜色?
【发布时间】:2014-09-25 05:25:24
【问题描述】:

我做了一个使用 ActionBarCompat 的应用程序

我使用 SlidingTabLayout 类创建了选项卡。

课程是这样的:

SlidingTabLayout.java

但我无法更改标签的颜色...

我的 viewpager 片段是这样的:

<swmovil.fyb.SlidingTabLayout
    android:id="@+id/mTabs"
    android:layout_width="match_parent"
    android:layout_height="48dip" />

<android.support.v4.view.ViewPager
    android:id="@+id/mPager"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="1"
    android:background="@color/white" />

应用程序运行良好,但我无法更改选项卡的彩色文本...

看到下面的例子,我就申请了:

rudsonlive/Navigation-Drawer-ViewPager-ActionBarCompat

如何更改标签文本的文本颜色?

谢谢!!!

【问题讨论】:

    标签: java android android-actionbar android-viewpager android-actionbar-compat


    【解决方案1】:

    打开你的文件SlidingTabLayout.java(来自Google IO的默认文件)并找到函数populateTabStrip(),然后在这段代码之后

    mTabStrip.addView(tabView);
            if (i == mViewPager.getCurrentItem()) {
                tabView.setSelected(true);
            }
    

    添加以下行:

    int color = ContextCompat.getColor(tabView.getContext(), R.color.grey);
    tabTitleView.setTextColor(color);
    

    用您喜欢的颜色替换R.color.grey

    【讨论】:

    • 是的。我也这么认为。
    【解决方案2】:
       @Override
        public void onPageSelected(int position) {
    
    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
    
      TextView tv = (TextView) mTabStrip.getChildAt(i);
     if (i==position)
      tv.setTextColor(getResources().getColorStateList(R.color.white));
     else                             
      tv.setTextColor(getResources().getColorStateList(R.color.tab_text_color));
    
            }
    

    这可能对你有帮助

    【讨论】:

      【解决方案3】:

      复制slidingtablayout和slidingtabstrip的代码并将其放入java文件中。然后在你的布局文件夹中创建一个customtab_title.xml,在你的drawable文件夹中创建一个selector.xml文件。 `

      <?xml version="1.0" encoding="utf-8"?>
              <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:orientation="horizontal"
                 android:padding="10dp"
         >
      
      
      <TextView
          android:id="@+id/textView2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="New Text"
          android:textColor="@drawable/slidingtab_title_color"/>
      
      
      </LinearLayout>
      

      选择器.xml

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
          <item android:state_selected="true" android:color="@color/unpressed" />
          <item android:state_focused="true" android:color="@color/unpressed" />
          <item android:state_pressed="true" android:color="@color/unpressed" />
          <item android:color="@android:color/black" />
      </selector> 
      

      在你的 mainactivity 或者你显示标签的地方添加一行代码 - tabs.setCustomTabView(R.layout.customtab_title, R.id.textView2);

      这里的tabs是slidingtablayout tabs;

      更改指示器颜色添加 - tabs.setSelectedIndicatorColors(getResources().getColor(R.color.unpressed));

      【讨论】:

      • 小评论,您的文本视图不必在线性布局中。它可以是 xml 中的唯一视图。
      【解决方案4】:

      1) 首先在res(/res/color)下创建color文件夹
      2) 在/res/color文件夹下创建xml文件selector.xml

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_selected="true" android:color="@android:color/white" />
      <item android:state_focused="true" android:color="@android:color/white" />
      <item android:state_pressed="true" android:color="@android:color/white" />
      <item android:color="#504f4f" /> 
      </selector> 
      

      3) 然后在SlidingTabLayout的populateTabStrip()方法中放这个

      tabTitleView.setTextColor(getResources().getColorStateList(R.color.selector));
      

      现在你有了一个选择器,你可以在任何你想要的事件上更改文本的颜色

      如果这不起作用,请添加以下代码行。
      a) 在最后的 populateTabStrip() 方法中添加这个

      if (i == mViewPager.getCurrentItem()) {
          tabView.setSelected(true);
      }
      

      和 b) 将 onPageSelected() 方法更改为此

          @Override
          public void onPageSelected(int position) {
              if (mScrollState == ViewPager.SCROLL_STATE_IDLE) {
                  mTabStrip.onViewPagerPageChanged(position, 0f);
                  scrollToTab(position, 0);
              }
              for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                  mTabStrip.getChildAt(i).setSelected(position == i);
              }
              if (mViewPagerPageChangeListener != null) {
                  mViewPagerPageChangeListener.onPageSelected(position);
              }
          }    
      

      【讨论】:

      • 你能帮我改变点击时的免费颜色吗?目前它显示默认的全息蓝色。
      • @AtulOHolic 我不知道该怎么做。对不起
      • 没有问题。非常感谢以上。这是一个很大的帮助。 :) 保持良好的工作。 :)
      • 直到我意识到需要使用项目的包而不是 android 包之前,它对我来说效果不佳: tabTitleView.setTextColor(getResources().getColorStateList( .R.color.selector));
      • @AtulOHolic 也许你没有弄清楚如何设置背景。在代码中使用setTextColor() 方法的相同位置,您可以调用tabTitleView.setBackground(getResources().getDrawable(R.drawable.selector_tab));
      【解决方案5】:

      不幸的是,此类不支持在不编辑代码的情况下自定义选项卡文本颜色,并且始终使用主题的默认文本颜色。您必须修补类以允许通过代码或样式属性设置选项卡文本颜色。 一种替代方法是使用PagerSlidingTabStrip 库。

      【讨论】:

        【解决方案6】:

        我使用 Panayiotis Irakleous 解决方案,但我认为最好避免在 onPageSelected 过程中循环部分。

        步骤相同,需要添加一个int类成员(例如:mCurrentTabIndex)来保存当前的标签索引。

        在步骤3.a中,您需要添加

        mCurrentTabIndex = i;
        

        所以它会是这样的:

        if (i == mViewPager.getCurrentItem()) {
            tabView.setSelected(true);
            mCurrentTabIndex = i;
        }
        

        最后,在步骤 3.b 中,将循环部分替换为:

        mTabStrip.getChildAt(mCurrentTabIndex).setSelected(false);
        mTabStrip.getChildAt(position).setSelected(true);
        mCurrentTabIndex = position;
        

        所以代码会是这样的:

        @Override
        public void onPageSelected(int position) {
            if (mScrollState == ViewPager.SCROLL_STATE_IDLE) {
                mTabStrip.onViewPagerPageChanged(position, 0f);
                scrollToTab(position, 0);
            }
        
            mTabStrip.getChildAt(mCurrentTabIndex).setSelected(false);
            mTabStrip.getChildAt(position).setSelected(true);
            mCurrentTabIndex = position;
        
            if (mViewPagerPageChangeListener != null) {
                mViewPagerPageChangeListener.onPageSelected(position);
            }
        }    
        

        【讨论】:

          【解决方案7】:

          查看 SlidingTabLayout 的代码...您可以设置自定义选项卡视图,它允许您控制选项卡的内容并设置自定义选项卡文本颜色。看看slidingTabLayout.setCustomTabView(int layoutResId, int textViewId)。

          【讨论】:

            【解决方案8】:

            您应该能够看到该类正在使用的 TextView。

            tabTitleView.setTextColor(getResources().getColor(R.color.white));
            

            在我的课堂上,TextView 是 tabTitleView。如果您使用的是 Google 提供的默认示例,您会在 populateTabStrip 函数下找到它。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-02-19
              • 2018-12-18
              • 1970-01-01
              • 1970-01-01
              • 2012-08-30
              • 2019-09-09
              相关资源
              最近更新 更多