以编程方式将State"#e3bb87"添加到StateListDrawable

Ray*_*non 3 android textview android-selector android-drawable

我需要以编程方式执行此操作的原因是文本颜色已下载,而不是在xml中预先定义.我以编程方式读取此 替换选择器图像

我只需要知道

StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
    getResources().getDrawable(R.drawable.pressed));
Run Code Online (Sandbox Code Playgroud)

如何变成

states.addState(new int[] {android.R.attr.state_pressed},**theMethodImLookingFor**("#e3bb87"));
Run Code Online (Sandbox Code Playgroud)

忘了getResources().getColor(R.color.anycolor),颜色没有在xml中定义

npa*_*ace 5

你可以用这个:

states.addState(new int[] {android.R.attr.state_pressed},
    new ColorDrawable(Color.parseColor("#e3bb87")));
Run Code Online (Sandbox Code Playgroud)