[Android]NumberPicker selected item change color

后端 未结 4 588
星月不相逢
星月不相逢 2021-01-17 20:09

Is it possible to change color of the selected item in numberpicker so each time a new center child TextView appear change its color to whatever I like I did not find any st

4条回答
  •  北海茫月
    2021-01-17 20:44

    By design is not possible to change only the center color. If you check the NumberPicker's source code, specifically in the onDraw method, then you will see that the widget makes no difference between the center item and the rest of the items when rendering. All the texts are rendered using the same color.

    Source code link. Check the onDraw method starting at the comment "//draw the selector wheel": https://android.googlesource.com/platform/frameworks/base/+/0e2d281/core/java/android/widget/NumberPicker.java

    In addition, when editing, it places an EditText control on top of the NumberPicker center area. Changing such EditText control will not solve the problem, even if you perform some tricks such as doing a programmatic click to set the EditText into focus, because as soon as you scroll again or set the focus to a different View in your layout, the EditText will be hidden, and you will loose such color effect.

    The only solution is to create a new NumberPicker control. For example you could use the actual NumberPicker source code, and adapt it to your needs. You will need to make the adjustments in the onDraw method.

提交回复
热议问题