Add floating point value to android resources/values

后端 未结 10 1896
清酒与你
清酒与你 2020-11-27 10:52

I\'m trying to add a little space between lines to my TextViews using android:lineSpacingMultiplier from the documentation:

Extra spacing

相关标签:
10条回答
  • 2020-11-27 11:03

    If you have simple floats that you control the range of, you can also have an integer in the resources and divide by the number of decimal places you need straight in code.

    So something like this

    <integer name="strokeWidth">356</integer>
    

    is used with 2 decimal places

    this.strokeWidthFromResources = resources_.getInteger(R.integer.strokeWidth);    
    circleOptions.strokeWidth((float) strokeWidthFromResources/ 100);
    

    and that makes it 3.56f

    Not saying this is the most elegant solution but for simple projects, it's convenient.

    0 讨论(0)
  • 2020-11-27 11:05

    Add a float to dimens.xml:

    <item format="float" name="my_dimen" type="dimen">1.2</item>

    To reference from XML:

    <EditText 
        android:lineSpacingMultiplier="@dimen/my_dimen"
        ...
    

    To read this value programmatically you can use ResourcesCompat.getFloat from androidx.core

    Gradle dependency:

    implementation("androidx.core:core:${version}")

    Usage:

    import androidx.core.content.res.ResourcesCompat;
    
    ...
    
    float value = ResourcesCompat.getFloat(context.getResources(), R.dimen.my_dimen);
    
    0 讨论(0)
  • 2020-11-27 11:06

    There is a solution:

    <resources>
        <item name="text_line_spacing" format="float" type="dimen">1.0</item>
    </resources>
    

    In this way, your float number will be under @dimen. Notice that you can use other "format" and/or "type" modifiers, where format stands for:

    Format = enclosing data type:

    • float
    • boolean
    • fraction
    • integer
    • ...

    and type stands for:

    Type = resource type (referenced with R.XXXXX.name):

    • color
    • dimen
    • string
    • style
    • etc...

    To fetch resource from code, you should use this snippet:

    TypedValue outValue = new TypedValue();
    getResources().getValue(R.dimen.text_line_spacing, outValue, true);
    float value = outValue.getFloat();  
    

    I know that this is confusing (you'd expect call like getResources().getDimension(R.dimen.text_line_spacing)), but Android dimensions have special treatment and pure "float" number is not valid dimension.


    Additionally, there is small "hack" to put float number into dimension, but be WARNED that this is really hack, and you are risking chance to lose float range and precision.

    <resources>
        <dimen name="text_line_spacing">2.025px</dimen>
    </resources>
    

    and from code, you can get that float by

    float lineSpacing = getResources().getDimension(R.dimen.text_line_spacing);
    

    in this case, value of lineSpacing is 2.024993896484375, and not 2.025 as you would expected.

    0 讨论(0)
  • 2020-11-27 11:09

    Although I've used the accepted answer in the past, it seems with the current Build Tools it is possible to do:

       <dimen name="listAvatarWidthPercent">0.19</dimen>
    

    I'm using Build Tools major version 29.

    0 讨论(0)
  • 2020-11-27 11:13

    I also found a workaround which seems to work fine with no warnings:

    <resources>
        <item name="the_name" type="dimen">255%</item>
        <item name="another_name" type="dimen">15%</item>
    </resources>
    

    Then:

    // theName = 2.55f
    float theName = getResources().getFraction(R.dimen.the_name, 1, 1);
    // anotherName = 0.15f
    float anotherName = getResources().getFraction(R.dimen.another_name, 1, 1);
    

    Warning : it only works when you use the dimen from Java code not from xml

    0 讨论(0)
  • 2020-11-27 11:16

    As described in this link http://droidista.blogspot.in/2012/04/adding-float-value-to-your-resources.html

    Declare in dimen.xml

    <item name="my_float_value" type="dimen" format="float">9.52</item>
    

    Referencing from xml

    @dimen/my_float_value
    

    Referencing from java

    TypedValue typedValue = new TypedValue();
    getResources().getValue(R.dimen.my_float_value, typedValue, true);
    float myFloatValue = typedValue.getFloat();
    
    0 讨论(0)
提交回复
热议问题