Dynamic Text size change according to tablet

前端 未结 4 1615
独厮守ぢ
独厮守ぢ 2020-12-14 03:51

With all effort,I finally reached to the end of my first app in android. And thanks to all. But after coming to end, I realized one thing that my app text size is common in

相关标签:
4条回答
  • 2020-12-14 04:38

    I was Having Same Problem but what i realized sp/db are not efficient for dealing with this kind of problem, I handled my alignment and font size related stuff programtically.

    here are the steps, how i handled this problem.

    • Calculate Screen Width and Height of Your device
    • Use Set TextView.setTextSize(unit, size), where the unit parameter is TypedValue.COMPLEX_UNIT_PX and the size parameter is percent of the total width of your screen. Normally, for me, .5% of total width for font is suitable. You can experiment by trying other total width percentages, such as 0.4% or 0.3%.

    This way your font size will be always suitable for each and every text/screen size.

    0 讨论(0)
  • 2020-12-14 04:47

    After checking some resources I finally came up with the following solution: Within the layout-xml I define the size of the button-text with a dimension declaration:

    android:textSize="@dimen/campaign_textfontsize"
    

    I created a dimens.xml in the "/values"-subdirectory with the corresponding value

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

    then I created a second values-folder "/values-sw720dp" and copied the old dimens.xml into it. In this dimens.xml I adapted the fontsize value:

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

    Based on the current screensize android selects the proper dimens-value. So without any code, font is adapted to the display- (and display related button-) size.

    Hope this helps...

    0 讨论(0)
  • 2020-12-14 04:50

    no need to change textsize dynamically use sp for textsize it will automatically arrange the text size depending on the screen resolutions..like in phone.

    android:textSize="10sp"
    
    0 讨论(0)
  • 2020-12-14 04:55

    It's quite an old question, but another modern solution can be really handy.
    With the Support library, 26+ new text auto sizing is available.
    In simple words, you define TextView size and text size gets automatically increased/decreased to match it's bounds:

    <TextView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:autoSizeTextType="uniform"
        app:autoSizeMinTextSize="12sp"
        app:autoSizeMaxTextSize="100sp"
        app:autoSizeStepGranularity="2sp" />
    

    More on Autosizing TextViews

    Add here the ability to set TextView size in a percentage of the screen with the ConstraintLayout, and you can create layouts which look almost identically on any device.

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