且构网

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

在运行时更改 Android 上的渐变背景颜色

更新时间:2023-12-06 15:06:04

是的!找到了方法!

不得不忘记 XML,但我是这样做的:

Had to forget about XML, but here's how I did it:

在我的 getView() 重载函数 (ListAdapter) 上,我只需要:

On my getView() overloaded function (ListAdapter) I just had to:

    int h = v.getHeight();
    ShapeDrawable mDrawable = new ShapeDrawable(new RectShape());
    mDrawable.getPaint().setShader(new LinearGradient(0, 0, 0, h, Color.parseColor("#330000FF"), Color.parseColor("#110000FF"), Shader.TileMode.REPEAT));
    v.setBackgroundDrawable(mDrawable);

这给了我与上面的 XML 背景相同的结果.现在我可以通过编程方式设置背景颜色.

And that gave me the same result as the XML background above. Now I can programmatically set the background color.