将浮点值添加到android资源/值

slu*_*lup 224 floating-point resources android

我试图线使用我的TextViews之间添加一点空间android:lineSpacingMultiplier文档:

文本行之间的额外间距,作为乘数.

必须是浮点值,例如"1.2".

由于我在几个不同的TextView中使用它,我想为我的资源添加一个全局维度/值,但我不知道使用哪个标记,如果它甚至存在.我已经尝试了所有对我有意义的资源类型,但它们都不起作用.

我想拥有的是这样的:

<resources>
    <dimen name="text_line_spacing">1.4</dimen>
</resources>
Run Code Online (Sandbox Code Playgroud)

编辑:我知道android:lineSpacingExtra(需要一个附加单位的维度),但我想android:lineSpacingMultiplier尽可能使用.

Tom*_*omo 492

有一个解决方案:

<resources>
    <item name="text_line_spacing" format="float" type="dimen">1.0</item>
</resources>
Run Code Online (Sandbox Code Playgroud)

这样,您的浮点数将在@dimen下.请注意,您可以使用其他"格式"和/或"类型"修饰符,其中format代表:

Format =封闭数据类型:

  • 浮动
  • 布尔
  • 分数
  • 整数
  • ...

和类型代表:

Type =资源类型(以R.XXXXX.name引用):

  • 颜色
  • 地扪
  • 样式
  • 等等...

要从代码中获取资源,您应该使用以下代码段:

TypedValue outValue = new TypedValue();
getResources().getValue(R.dimen.text_line_spacing, outValue, true);
float value = outValue.getFloat();  
Run Code Online (Sandbox Code Playgroud)

我知道这很混乱(你希望看起来像getResources().getDimension(R.dimen.text_line_spacing)),但Android dimensions有特殊的处理和纯粹的"浮动"数字是无效的维度.


此外,还有小"黑客"把浮点数到尺寸,但被警告,这是真正的破解,你处于危险之中的机会失去浮动的范围和精度.

<resources>
    <dimen name="text_line_spacing">2.025px</dimen>
</resources>
Run Code Online (Sandbox Code Playgroud)

从代码中,你可以得到那个浮动

float lineSpacing = getResources().getDimension(R.dimen.text_line_spacing);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,价值lineSpacing2.024993896484375,而不是2.025你所期望的.

  • @rodkarom从xml中检索浮点数,使用这个XML:`<item type ="string"format ="float"name ="my_float"> 0.5 </ item>`和这段代码来检索它:`float my_float = Float. parseFloat(getResources().getString(R.string.my_float));` (19认同)
  • 我认为你应该删除hackish部分,sollution是完美的,因为它是. (4认同)
  • 我尝试使用它,但在从代码中检索值时得到 NotFoundException,“类型 #0x04 无效”。 (2认同)

yaj*_*esh 76

如此链接中所述http://droidista.blogspot.in/2012/04/adding-float-value-to-your-resources.html

在dimen.xml中声明

<item name="my_float_value" type="dimen" format="float">9.52</item>
Run Code Online (Sandbox Code Playgroud)

从xml引用

@dimen/my_float_value
Run Code Online (Sandbox Code Playgroud)

从java引用

TypedValue typedValue = new TypedValue();
getResources().getValue(R.dimen.my_float_value, typedValue, true);
float myFloatValue = typedValue.getFloat();
Run Code Online (Sandbox Code Playgroud)


waq*_*lam 38

所有解决方案都建议您通过代码使用预定义的浮点值.

但是如果你想知道如何在XML中引用预定义的浮点值(例如布局),那么下面就是我所做的一个例子,它完美地工作:

将资源值定义为type="integer"但是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>
Run Code Online (Sandbox Code Playgroud)

然后使用它们在布局中使用它们@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>
Run Code Online (Sandbox Code Playgroud)


Cri*_*ian 18

我还发现了一个似乎没有任何警告的解决方法:

<resources>
    <item name="the_name" type="dimen">255%</item>
    <item name="another_name" type="dimen">15%</item>
</resources>
Run Code Online (Sandbox Code Playgroud)

然后:

// 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);
Run Code Online (Sandbox Code Playgroud)

警告:它仅在您使用Java代码中的dimen而不是来自xml时才有效

  • 究竟.它只适用于使用Java代码中的dimen. (4认同)
  • 这是我从代码中访问浮点数的最佳方法. (2认同)

Ale*_*ker 17

向 dimens.xml 添加一个浮点数:

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

从 XML 引用:

<EditText 
    android:lineSpacingMultiplier="@dimen/my_dimen"
    ...
Run Code Online (Sandbox Code Playgroud)

要以编程方式读取此值,您可以ResourcesCompat.getFloat从 androidx.core使用

Gradle依赖:

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

用法:

import androidx.core.content.res.ResourcesCompat;

...

float value = ResourcesCompat.getFloat(context.getResources(), R.dimen.my_dimen);
Run Code Online (Sandbox Code Playgroud)


Pho*_*int 7

我们也可以将其用作约束布局的准则。

创建integer.xml文件并添加到

 <item name="guideline_button_top" type="integer" format="float">0.60</item>
Run Code Online (Sandbox Code Playgroud)

从layout.xml文件使用

 app:layout_constraintGuide_percent="@integer/guideline_button_top" 
Run Code Online (Sandbox Code Playgroud)


Sco*_*ggs 5

我用一种风格来解决这个问题.官方链接在这里.

很有用的东西.您创建一个文件来保存样式(如"styles.xml"),并在其中定义它们.然后引用布局中的样式(如"main.xml").

这是一个样本样式,可以满足您的需求:

<style name="text_line_spacing">
   <item name="android:lineSpacingMultiplier">1.4</item>
</style>
Run Code Online (Sandbox Code Playgroud)

假设你想用这个改变一个简单的TextView.在您的布局文件中键入:

<TextView
   style="@style/summary_text"
   ...
   android:text="This sentence has 1.4 times more spacing than normal."
/>
Run Code Online (Sandbox Code Playgroud)

试试吧 - 这实际上就是如何在android上完成所有内置UI.通过使用样式,您还可以选择修改视图的各种其他方面.