How does one use Resources.getFraction()?

后端 未结 1 1021
时光说笑
时光说笑 2020-12-30 20:30

How do I store a fractional value like 3.1416 in resources? What to write in the XML and how to retrieve it in Java code?

The documentation for getFract

相关标签:
1条回答
  • 2020-12-30 21:07

    You specify fractions in XML as so:

       <item name="fraction" type="fraction">5%</item>
       <item name="parent_fraction" type="fraction">2%p</item>
    

    Where 5% would be 0.05 when actually used.

    Then:

    // 0.05f
    getResources().getFraction(R.fraction.fraction, 1, 1);
    // 0.02f
    getResources().getFraction(R.fraction.parent_fraction, 1, 1);
    // 0.10f
    getResources().getFraction(R.fraction.fraction, 2, 1);
    // 0.10f
    getResources().getFraction(R.fraction.fraction, 2, 2);
    // 0.04f
    getResources().getFraction(R.fraction.parent_fraction, 1, 2);
    // 0.04f
    getResources().getFraction(R.fraction.parent_fraction, 2, 2);
    

    As you can see, depending on the type of fraction, the getFraction method multiples the values accordingly. If you specify a parent fraction (%p), it uses the second argument (pbase), ignoring the first. On the other hand, specifying a normal fraction, only the base argument is used, multiplying the fraction by this.

    0 讨论(0)
提交回复
热议问题