Android float/double resource type

前端 未结 3 1308
囚心锁ツ
囚心锁ツ 2020-12-30 00:04

As I understood from available Android Resource types, there is no straight way to use float values as resources, unless you use some hacks such as the one mentioned in here

3条回答
  •  情歌与酒
    2020-12-30 00:15

    Just save your double as a String resource

    0.12154646
    

    And then just parse that in your code like this

    double some_decimal = Double.parseDouble(context.getString(R.string.some_decimal));
    

    You can also make your own type of resource values and get it from there like this

    2.0
    

    And then get it like this

    TypedValue tempVal = new TypedValue();
    getResources().getValue(R.vals.some_decimal, tempVal, true);
    float some_decimal = tempVal.getFloat();
    

    But it's impossible to get doubles like this and also I think that it's less performant than just simply parsing a string resource, so I prefer my first option.

提交回复
热议问题