gpt4 book ai didi

android - 如何使用 Resources.getFraction()?

转载 作者:IT老高 更新时间:2023-10-28 22:06:39 24 4
gpt4 key购买 nike

如何在资源中存储像 3.1416 这样的小数值?在 XML 中写什么以及如何在 Java 代码中检索它?

getFraction() 的文档状态:

public float getFraction (int id, int base, int pbase)

Retrieve a fractional unit for a particular resource ID.

Parameters
base The base value of this fraction. In other words, a standard fraction is multiplied by this value.
pbase The parent base value of this fraction. In other words, a parent fraction (nn%p) is multiplied by this value.

Returns
Attribute fractional value multiplied by the appropriate base value

This answer显示了一个简单的百分比示例,而没有详细说明参数的含义。

最佳答案

您在 XML 中这样指定分数:

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

实际使用时 5% 为 0.05。

然后:

// 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);

如您所见,根据分数的类型,getFraction 方法会相应地乘以这些值。如果您指定一个父分数 (%p),它将使用第二个参数 (pbase),而忽略第一个。另一方面,指定一个普通分数,只使用 base 参数,将分数乘以这个。

关于android - 如何使用 Resources.getFraction()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11734470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com