zoukankan      html  css  js  c++  java
  • 在带(继承)TextView的控件中,在代码中动态更改TextView的文字颜色

    今天由于公司项目需求,须要实现一种类似tab的选项卡,当时直接想到的就是使用RadioGroup和RadioButton来实现。

    这种方法全然没问题。可是在后来的开发过程中,却遇到了一些困扰非常久的小困难。大概需求是:在代码中。动态的获取tab的个数,然后初始化RadioGroup,每个tab相应一个RadioButton,即加入一个tab就要向RadioGroup中add一个RadioButton,然后在button选中时要更改文字颜色。由于是动态加入,所以无法在xml中配置了RadioButton和设置selector来更改文字颜色了。

    以下贴上部分代码:

            int size = lvl1.size();
            for (int i = 0; i < size; i++) {
                Question q = lvl1.get(i);
                RadioButton btn = getRadioButton(q, i);
                radioGroup.addView(btn,
                        new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT,
                                RadioGroup.LayoutParams.MATCH_PARENT, 1));
                if(i == 0){
                    subLevel.clear();
                    subLevel.addAll(q.getSubLevel());
                    adapter.notifyDataSetChanged();    // 数据变化了
                    btn.setChecked(true);
                }
            }

    先看看上面这段代码。我通过getRadioButton()来动态生成一个RadioButton。然后调用addView()加入到RadioGroup中,此时,须要注意的是:

     radioGroup.addView(btn,
                        new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT,
                                RadioGroup.LayoutParams.MATCH_PARENT, 1));
    方法中指定了RadioButton的布局參数,要注意。这里一定要使用RadioGroup.LayoutParams这个类来指定RadioButton的布局參数,假设你使用其它的比如:

    LinearLayout.LayoutParams、ViewGroup.LayoutParams等类,将不会起作用!!

    这就是我第一个遇到的小坑。为什么会导致这样呢?由于在RadioGroup中,它已经重写了LayoutParams了。当时我大概看了一下源代码。预计就是在这里面有变动造成的。

    再来看看,getRadioButton这种方法:

        private RadioButton getRadioButton(Question q, int index)
        {
            RadioButton btn = new RadioButton(context);
            btn.setId(index);
            Drawable d = context.getResources().getDrawable(R.drawable.radiogroup_tab_selector);
            d.setBounds(0, 25, 150, 55);
            btn.setButtonDrawable(R.drawable.transparent);
            btn.setCompoundDrawables(null, null, null, d);
            btn.setText(q.getQs_content());
            btn.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
            /* 在代码中使用selector来改变选中button文字颜色。须要使用getColorStateList(int id),这种方法来解析我们定义selector。
               使用getColor()是解析不出来的。

    */ btn.setTextColor(context.getResources().getColorStateList(R.color.acs_tab_textcolor_selector)); return btn; }

    上面代码中有两个地方要注意:

    1. 在调用setButtonDrawables()时。须要先调用了Drawable的setBounds()方法,这样设置的Drawable才可以显示出来

    2. 在代码中。使用selector来动态改变RadioButton选中时文字颜色。须要使用getColorStateList(int id)。这种方法来解析我们定义selector,使用getColor()是解析不出来的。


    总结:尽管上面我是以RadioButton作为样例,可是,仅仅要是继承TextView的控件,须要在代码中动态改变文本颜色。都须要注意上面提到的地方。


  • 相关阅读:
    零基础学python-2.6 数字与字符串
    零基础学python-2.5 变量与赋值
    零基础学python-2.4 操作符
    零基础学python-2.3 注释
    js原型和原型链
    ES6数据结构Set、Map
    JS高阶函数--------map、reduce、filter
    vue-cli3.0以上项目中引入jquery的方法
    vue项目中使用echarts map报错Cannot read property 'push' of undefined nanhai.js
    js 将时间戳转成时间格式化
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/6964803.html
Copyright © 2011-2022 走看看