How does one use Resources.getFraction()

本文详细介绍了如何使用Android资源系统存储和获取小数分数值,包括XML配置和Java代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


转:http://stackoverflow.com/questions/11734470/how-does-one-use-resources-getfraction

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 getFraction() states:

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 shows a simple example of percentages without going into the details of what the arguments mean.

share improve this question
 

1 Answer

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.

vite v5.4.19 building for production... transforming (7) node_modules\vue\dist\vue.runtime.esm-bundler.js[@vue/compiler-sfc] ::v-deep usage as a combinator has been deprecated. Use :deep(<inner-selector>) instead of ::v-deep <inner-selector>. [@vue/compiler-sfc] ::v-deep usage as a combinator has been deprecated. Use :deep(<inner-selector>) instead of ::v-deep <inner-selector>. [@vue/compiler-sfc] ::v-deep usage as a combinator has been deprecated. Use :deep(<inner-selector>) instead of ::v-deep <inner-selector>. ✓ 136 modules transformed. dist/index.html 0.43 kB │ gzip: 0.28 kB dist/assets/index-fRN9Gjbe.css 3.24 kB │ gzip: 1.18 kB dist/assets/home-DQ_X9V7Z.css 7.87 kB │ gzip: 1.90 kB dist/assets/index-BweWhkAY.js 92.14 kB │ gzip: 36.63 kB dist/assets/home-BPoxZqS0.js 199.11 kB │ gzip: 69.78 kB ✓ built in 1.29s • electron-builder version=24.13.3 os=10.0.26100 • loaded configuration file=package.json ("build" field) ⨯ Invalid configuration object. electron-builder 24.13.3 has been initialized using a configuration object that does not match the API schema. - configuration.extraResources[0] has an unknown property 'noExtract'. These properties are valid: object { filter?, from?, to? } How to fix: 1. Open https://www.electron.build/configuration/configuration 2. Search the option name on the page (or type in into Search to find across the docs). * Not found? The option was deprecated or not exists (check spelling). * Found? Check that the option in the appropriate place. e.g. "title" only in the "dmg", not in the root. - configuration.win.extraResources[0] has an unknown property 'noExtract'. These properties are valid: object { filter?, from?, to? } How to fix: 1. Open https://www.electron.build/configuration/win 2. Search the option name on the page (or type in into Search to find across the docs). * Not found? The option was deprecated or not exists (check spelling). * Found? Check that the option in the appropriate place. e.g. "title" only in the "dmg", not in the root. failedTask=build stackTrace=ValidationError: Invalid configuration object. electron-builder 24.13.3 has been initialized using a configuration object that does not match the API schema. - configuration.extraResources[0] has an unknown property 'noExtract'. These properties are valid: object { filter?, from?, to? } How to fix: 1. Open https://www.electron.build/configuration/configuration 2. Search the option name on the page (or type in into Search to find across the docs). * Not found? The option was deprecated or not exists (check spelling). * Found? Check that the option in the appropriate place. e.g. "title" only in the "dmg", not in the root. - configuration.win.extraResources[0] has an unknown property 'noExtract'. These properties are valid: object { filter?, from?, to? } How to fix: 1. Open https://www.electron.build/configuration/win 2. Search the option name on the page (or type in into Search to find across the docs). * Not found? The option was deprecated or not exists (check spelling). * Found? Check that the option in the appropriate place. e.g. "title" only in the "dmg", not in the root. at validate (D:\admin-project\guangxi-quan-vue3\node_modules\@develar\schema-utils\dist\validate.js:86:11) at validateConfig (D:\admin-project\guangxi-quan-vue3\node_modules\app-builder-lib\src\util\config.ts:240:3) at Packager._build (D:\admin-project\guangxi-quan-vue3\node_modules\app-builder-lib\src\packager.ts:345:5) at Packager.build (D:\admin-project\guangxi-quan-vue3\node_modules\app-builder-lib\src\packager.ts:340:12) at executeFinally (D:\admin-project\guangxi-quan-vue3\node_modules\builder-util\src\promise.ts:12:14)
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值