android eclipse using string resource for text size in layouts

后端 未结 2 702
攒了一身酷
攒了一身酷 2021-01-13 04:23

I want to be able to store a font size (18sp) in a resource so that a lot of my layouts will use so that I can easily change the size in the future if I need to. I\'ve a st

相关标签:
2条回答
  • 2021-01-13 04:58

    Create a dimens.xml file in your res directory with contents similar to this:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <dimen name="some_text_size">18dp</dimen>
    </resources>
    

    Then instead of referencing it like @string/blah, use @dimen/blah.

    0 讨论(0)
  • 2021-01-13 05:07

    Use this as your XML resource and call it from where ever you want...

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <dimen name="textview_height">25dp</dimen>
        <dimen name="textview_width">150dp</dimen>
        <dimen name="ball_radius">30dp</dimen>
        <dimen name="font_size">16sp</dimen>
    </resources>
    

    Then call this in code:

    Resources res = getResources();
    float fontSize = res.getDimension(R.dimen.font_size);
    
    0 讨论(0)
提交回复
热议问题