且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何设置渐变为一个TextView的文本颜色?

更新时间:2023-01-27 20:36:24

  TextView的secondTextView =新的TextView(本);
着色textShader =新的LinearGradient(0,0,0,20,
        新的INT [] {Color.GREEN,Color.BLUE},
        新浮法[] {0,1},TileMode.CLAMP);
secondTextView.getPaint()setShader(textShader)。
 

I have a file containing a gradient( textgradient.xml) in my drawable folder. I need to put this gradient as the text color of a TextView through Java. How to do that?

TextView secondTextView = new TextView(this);
Shader textShader=new LinearGradient(0, 0, 0, 20,
        new int[]{Color.GREEN,Color.BLUE},
        new float[]{0, 1}, TileMode.CLAMP);
secondTextView.getPaint().setShader(textShader);