【问题标题】:How to change tab background color programmatically?如何以编程方式更改选项卡背景颜色?
【发布时间】:2015-11-24 12:50:32
【问题描述】:

我正在尝试更改所选(活动选项卡)的背景颜色。我的情况是默认情况下,当用户选择它得到我想要的颜色的选项卡时,所有选项卡都是白色的。 我正在使用库名称 PagerSlidingTabStrip ...到目前为止我尝试的是:

在标签更改时,我调用此代码。它工作得很好,可以改变文本颜色。现在我只想更改相同的选项卡背景颜色。

private void setSelectedTabColor(int position) {
    for(int i=0; i<tabStrip.getChildCount(); i++)  
    {  
        LinearLayout view = (LinearLayout) tabStrip.getChildAt(0);
        TextView textView = (TextView) view.getChildAt(i);
        textView.setTextColor(getResources().getColor(R.color.black)); 
    }  

    LinearLayout view = (LinearLayout) tabStrip.getChildAt(0);
    TextView textView = (TextView) view.getChildAt(position);
    textView.setTextColor(getResources().getColor(R.color.white));
}

通过循环尝试下面的代码,我得到空指针异常。

tabStrip.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.dash_bar));

【问题讨论】:

  • 发布tabStrip循环(我不是反对者)
  • 它在 setSelectedTabColor 函数中看上面。
  • userBabies 的循环不是 tabStrip 的循环
  • userBabies 是 tabsCOunt
  • 试试看:textView.getParent().setBackgroundColor(getResources().getColor(R.color.dash_bar));

标签: android tabs pagerslidingtabstrip


【解决方案1】:

添加这些行来改变特定孩子的背景。

LinearLayout view = (LinearLayout) tabStrip.getChildAt(0);      
TextView textView = (TextView) view.getChildAt(position);
textView.getParent().setBackgroundColor(getResources().getColor(
              R.color.dash_bar));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多