我试图添加一个小的行之间的空间到我的TextViews使用android:lineSpacingMultiplier 从文档中可以看到:

文本行与行之间的额外间距, 作为乘数。 必须是浮点值,比如 “1.2”。

因为我使用这在几个不同的TextViews,我想添加一个全局维度/值到我的资源,但我不知道使用哪个标签,如果它甚至存在。 我尝试了所有对我有意义的资源类型,但没有一个有用。

我想要的是这样的:

<resources>
    <dimen name="text_line_spacing">1.4</dimen>
</resources>

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


当前回答

我们也可以用它作为约束布局的指导原则。

创建integer.xml文件并添加到

 <item name="guideline_button_top" type="integer" format="float">0.60</item>

从layout.xml文件中使用

 app:layout_constraintGuide_percent="@integer/guideline_button_top" 

其他回答

如果有人对“欺骗系统”感兴趣,当你被允许只给出特定类型的xml值时,特别是在android布局格式化中使用xml值作为嵌入权重时,你可以这样做:

    <item name="actvity_top_panel_weight" format="float" type="integer">1.5</item>

上面的xml值类似于浮点数,但被称为整数。

然后你可以像下面这样毫无问题地使用它:

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="@integer/actvity_top_panel_weight"
        android:orientation="horizontal"></LinearLayout>

在dimensions .xml中添加一个浮点数:

<项目格式=“浮点数” 名称=“my_dimen” 类型=“dimen”>1.2</item>

从XML引用:

<EditText 
    android:lineSpacingMultiplier="@dimen/my_dimen"
    ...

要以编程方式读取这个值,可以使用ResourcesCompat。getFloat从androidx.core

Gradle依赖性:

实现(“androidx.core:核心:$ {version}”)

用法:

import androidx.core.content.res.ResourcesCompat;

...

float value = ResourcesCompat.getFloat(context.getResources(), R.dimen.my_dimen);

我们也可以用它作为约束布局的指导原则。

创建integer.xml文件并添加到

 <item name="guideline_button_top" type="integer" format="float">0.60</item>

从layout.xml文件中使用

 app:layout_constraintGuide_percent="@integer/guideline_button_top" 

如果您有控制范围的简单浮点数,您也可以在资源中使用一个整数,并在代码中直接除以所需的小数点后数位。

就像这样

<integer name="strokeWidth">356</integer>

小数点后2位

this.strokeWidthFromResources = resources_.getInteger(R.integer.strokeWidth);    
circleOptions.strokeWidth((float) strokeWidthFromResources/ 100);

所以是3.56f

并不是说这是最优雅的解决方案,但对于简单的项目来说,它很方便。

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

在dimension .xml中声明

<item name="my_float_value" type="dimen" format="float">9.52</item>

从xml引用

@dimen/my_float_value

从java引用

TypedValue typedValue = new TypedValue();
getResources().getValue(R.dimen.my_float_value, typedValue, true);
float myFloatValue = typedValue.getFloat();