Android get color as string value

前端 未结 11 1674
青春惊慌失措
青春惊慌失措 2021-01-30 19:28

If i defined a color in resources


    #123456

it\'s possible

11条回答
  •  不知归路
    2021-01-30 19:58

    All of the solutions here using Integer.toHexString() break if you would have leading zeroes in your hex string. Colors like #0affff would result in #affff. Use this instead:

    String.format("#%06x", ContextCompat.getColor(this, R.color.your_color) & 0xffffff)
    

    or with alpha:

    String.format("#%08x", ContextCompat.getColor(this, R.color.your_color) & 0xffffffff)
    

提交回复
热议问题