将浮点值添加到 android 资源/值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3282390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add floating point value to android resources/values
提问by slup
I'm trying to add a little space between lines to my TextViews using android:lineSpacingMultiplier
from the documentation:
我试图线使用我的TextViews之间添加一点空间android:lineSpacingMultiplier
从文档:
Extra spacing between lines of text, as a multiplier.
Must be a floating point value, such as "1.2".
文本行之间的额外间距,作为乘数。
必须是浮点值,例如“1.2”。
As I'm using this in a few different TextViews I would like to add a global dimension/value to my resources, but I don't know which tag to use, if it even exists. I have tried all resource typesthat make sense to me, but none of them works.
当我在几个不同的 TextViews 中使用它时,我想为我的资源添加一个全局维度/值,但我不知道要使用哪个标签,如果它存在的话。我尝试了所有对我有意义的资源类型,但没有一个有效。
What I would like to have would be something like this:
我想要的是这样的:
<resources>
<dimen name="text_line_spacing">1.4</dimen>
</resources>
Edit: I'm aware of android:lineSpacingExtra
(which needs a dimension with an appended unit), but I'd like to use android:lineSpacingMultiplier
if possible.
编辑:我知道android:lineSpacingExtra
(需要一个带有附加单位的维度),但android:lineSpacingMultiplier
如果可能的话我想使用。
回答by Tomo
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:
这样,您的浮点数将在@dimen 下。请注意,您可以使用其他“格式”和/或“类型”修饰符,其中格式代表:
Format = enclosing data type:
格式 = 封闭数据类型:
- float
- boolean
- fraction
- integer
- ...
- 漂浮
- 布尔值
- 分数
- 整数
- ...
and type stands for:
和类型代表:
Type = resource type (referenced with R.XXXXX.name):
Type = 资源类型(以 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.
我知道这很令人困惑(您希望像 那样调用getResources().getDimension(R.dimen.text_line_spacing)
),但 Androiddimensions
有特殊处理,纯“浮点数”数字不是有效维度。
Additionally, there is small "hack" to put float number into dimension, but be WARNEDthat 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.
在这种情况下,值lineSpacing
是2.024993896484375
,而不是2.025
您所期望的。
回答by yajnesh
As described in this link http://droidista.blogspot.in/2012/04/adding-float-value-to-your-resources.html
如此链接中所述http://droidista.blogspot.in/2012/04/adding-float-value-to-your-resources.html
Declare in dimen.xml
在 dimen.xml 中声明
<item name="my_float_value" type="dimen" format="float">9.52</item>
Referencing from xml
从 xml 引用
@dimen/my_float_value
Referencing from java
从java引用
TypedValue typedValue = new TypedValue();
getResources().getValue(R.dimen.my_float_value, typedValue, true);
float myFloatValue = typedValue.getFloat();
回答by waqaslam
All the solutions suggest you to use the predefined float value through code.
所有解决方案都建议您通过代码使用预定义的浮点值。
But in case you are wondering how to reference the predefined float value in XML (for example layouts), then following is an example of what I did and it's working perfectly:
但是,如果您想知道如何在 XML 中引用预定义的浮点值(例如布局),那么以下是我所做的并且运行良好的示例:
Define resource values as type="integer"
but format="float"
, for example:
将资源值定义为type="integer"
but format="float"
,例如:
<item name="windowWeightSum" type="integer" format="float">6.0</item>
<item name="windowNavPaneSum" type="integer" format="float">1.5</item>
<item name="windowContentPaneSum" type="integer" format="float">4.5</item>
And later use them in your layout using @integer/name_of_resource
, for example:
稍后在您的布局中使用它们@integer/name_of_resource
,例如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="@integer/windowWeightSum" // float 6.0
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="@integer/windowNavPaneSum" // float 1.5
android:orientation="vertical">
<!-- other views -->
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="@integer/windowContentPaneSum" // float 4.5
android:orientation="vertical">
<!-- other views -->
</LinearLayout>
</LinearLayout>
回答by Cristian
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
警告:它仅在您使用 Java 代码而非 xml 中的维度时才有效
回答by Photon Point
We can also use it for the guideline of the constraint layout.
我们还可以将其用作约束布局的指南。
Create integer.xml file and add into
创建 integer.xml 文件并添加到
<item name="guideline_button_top" type="integer" format="float">0.60</item>
Use from a layout.xml file
从 layout.xml 文件使用
app:layout_constraintGuide_percent="@integer/guideline_button_top"
回答by Alex Baker
Add a float to dimens.xml:
向 dimens.xml 添加一个浮点数:
<item format="float" name="my_dimen" type="dimen">1.2</item>
<item format="float" name="my_dimen" type="dimen">1.2</item>
To reference from XML:
从 XML 引用:
<EditText
android:lineSpacingMultiplier="@dimen/my_dimen"
...
To read this value programmatically you can use ResourcesCompat.getFloat
from androidx.core
要以编程方式读取此值,您可以ResourcesCompat.getFloat
从 androidx.core使用
Gradle dependency:
Gradle依赖:
implementation("androidx.core:core:${version}")
implementation("androidx.core:core:${version}")
Usage:
用法:
import androidx.core.content.res.ResourcesCompat;
...
float value = ResourcesCompat.getFloat(context.getResources(), R.dimen.my_dimen);
回答by Scott Biggs
I used a style to solve this issue. The official link is here.
我使用了一种样式来解决这个问题。官方链接在这里。
Pretty useful stuff. You make a file to hold your styles (like "styles.xml"), and define them inside it. You then reference the styles in your layout (like "main.xml").
很实用的东西。您创建一个文件来保存您的样式(如“styles.xml”),并在其中定义它们。然后引用布局中的样式(如“main.xml”)。
Here's a sample style that does what you want:
这是一个可以满足您要求的示例样式:
<style name="text_line_spacing">
<item name="android:lineSpacingMultiplier">1.4</item>
</style>
Let's say you want to alter a simple TextView with this. In your layout file you'd type:
假设你想用这个来改变一个简单的 TextView。在您的布局文件中,您输入:
<TextView
style="@style/summary_text"
...
android:text="This sentence has 1.4 times more spacing than normal."
/>
Try it--this is essentially how all the built-in UI is done on the android. And by using styles, you have the option to modify all sorts of other aspects of your Views as well.
试试看——这基本上就是所有内置 UI 在 android 上完成的方式。通过使用样式,您还可以选择修改视图的各种其他方面。
回答by Thanasis Kapelonis
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
使用 2 位小数
this.strokeWidthFromResources = resources_.getInteger(R.integer.strokeWidth);
circleOptions.strokeWidth((float) strokeWidthFromResources/ 100);
and that makes it 3.56f
这使它成为3.56f
Not saying this is the most elegant solution but for simple projects, it's convenient.
并不是说这是最优雅的解决方案,但对于简单的项目,它很方便。
回答by slup
I found a solution, which works, but does result in a Warning
(WARN/Resources(268): Converting to float: TypedValue{t=0x3/d=0x4d "1.2" a=2 r=0x7f06000a}
) in LogCat.
我找到了一个有效的解决方案,但确实会在 LogCat中生成Warning
( WARN/Resources(268): Converting to float: TypedValue{t=0x3/d=0x4d "1.2" a=2 r=0x7f06000a}
)。
<resources>
<string name="text_line_spacing">1.2</string>
</resources>
<android:lineSpacingMultiplier="@string/text_line_spacing"/>
回答by androidguy
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.
我正在使用构建工具主要版本 29。