开发者社区> 问答> 正文

使用 XML 如何改变一个 tab 的背景?

在 TabHost 标签中包含一个 TabWidget 标签。我想自定义选项卡的选中和未被选中的状态
但是不知道如何把这些 xml 文件放在一起,从而来自定义选项卡?
screenshot
my selector xml:
screenshot
selected state:
screenshot
unselected state:
screenshot

展开
收起
蛮大人123 2016-02-15 15:24:11 2157 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    使用下面的代码,tw 指的是 TabWidget

    for (int i = 0; i < tw.getChildCount(); i++)
        {
            View v = tw.getChildAt(i);
            v.setBackgroundDrawable(null);
            v.setOnTouchListener(new OnTouchListener()
            {
    
                @Override
                public boolean onTouch(View v, MotionEvent event)
                {
    
                    // Change the icon and background colors
                    TabWidget tw = getTabWidget();
                    for (int i = 0; i < tw.getChildCount(); i++)
                    {
                        View vv = tw.getChildAt(i);
                        vv.setBackgroundDrawable(null);
                        vv.setId(0);
                    }
    
                    NonScalingBackgroundDrawable nsbd = new NonScalingBackgroundDrawable(getApplicationContext(), v, R.drawable.nav_highlight);
    
                    v.setBackgroundDrawable(nsbd);
    
                    v.setId(1);
                    return false;
    
                }
    
            });
        }
    }
    2019-07-17 18:43:19
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关课程

更多

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载