Add floating point value to android resources/values

后端 未结 10 1900
清酒与你
清酒与你 2020-11-27 10:52

I\'m trying to add a little space between lines to my TextViews using android:lineSpacingMultiplier from the documentation:

Extra spacing

相关标签:
10条回答
  • 2020-11-27 11:16

    We can also use it for the guideline of the constraint layout.

    Create integer.xml file and add into

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

    Use from a layout.xml file

     app:layout_constraintGuide_percent="@integer/guideline_button_top" 
    
    0 讨论(0)
  • 2020-11-27 11:19

    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:

    Define resource values as type="integer" but format="float", for example:

    <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:

    <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>
    
    0 讨论(0)
  • 2020-11-27 11:21

    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").

    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
       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.

    0 讨论(0)
  • 2020-11-27 11:21

    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.

    <resources>
        <string name="text_line_spacing">1.2</string>
    </resources>
    
    <android:lineSpacingMultiplier="@string/text_line_spacing"/>
    
    0 讨论(0)
提交回复
热议问题