Different text size for different hardware

后端 未结 3 997
长情又很酷
长情又很酷 2020-12-24 08:28

I have a edit text layout as below and I want to know, is there any way to provide different size for it for different hardware depending on its screen size? For screens bel

相关标签:
3条回答
  • 2020-12-24 08:59

    If you use <dimen> in a configuration dependent way, you will override default size for all devices (22dp for smaller, 40dp for larger devices). If you want to leave textSize as it was by default for smaller devices, and only override it for larger devices, you can use styles:

    res/values/styles.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="EditText" />      
    
    </resources>
    

    res/values-large/styles.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="EditText" >
          <item name="android:textSize">40sp</item>
        </style>    
    </resources>
    

    And in the layout:

    <TextView style="@style/EditText" ...  />
    

    This way textSize is left untouched for smaller devices, and default value from current theme is used.

    0 讨论(0)
  • 2020-12-24 09:01

    EDIT - the best way of doing this is to define the text sizes as a dimen and vary the sizes depending on what hardware you are targeting by putting different values in the dimens.xml file in your different dpi (ldpi, mdpi, hdpi etc) bucket folders.

    Old (correct but not great) answer

    2 ways of doing this :

    1. Remove the definition from the XML and set the text size programatically

      ((EditText) findViewById(R.id.entry)).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 40)
      
    2. implement different layouts for the different screen sizes - this will involve you writing a new xml layout for each of the different screen sizes you support in your application.

    0 讨论(0)
  • 2020-12-24 09:08

    Create folders in /res for

    layout

    layout-small

    layout-normal

    layout-large

    layout-xlarge

    then create a layout file for each layout, android will choose the right one on runtime. so you can change the complete layout for each screensize.

    If you really only want to set the size, you can create this folders

    values

    values-small

    values-normal

    values-large

    values-xlarge

    then put a dimen.xml in each of this folders like this :

    <?xml version="1.0" encoding="utf-8"?>

    <resources>

    <dimen name="textSizeLarge">22dp</dimen>

    </resources>

    just change the value (22dp) in each xml file.

    then replace your value with

    android:textSize="@dimen/textSizeLarge"

    Very good documantation : http://developer.android.com/guide/practices/screens_support.html

    0 讨论(0)
提交回复
热议问题