Android: How to change app font size from code

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 17:47:33

You can alter text size for textviews with

textview.setTextSize(int_value);

Is this what you meant?

Call setTextSize() on each TextView (or other widgets that inherit from TextView, such as Button).

The only way I can think of is to extend TextView and make them check a global textSize variable on every onDraw(). It should not add too much overhead to the drawing.

It seems like I cant change app font size and I have to change font size for individual element seperateley.

MoJa

You must create file styles.xml in values and in this file:

<resources>
    <style>
        <item name="android:textSize">20sp</item>
    </style>
</resources>

Define a style in styles.xml file under the values directory and customize whatever you want. I'm using textSize 11sp

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColor">@color/textColorTitle</item>
        <item name="android:textSize">11sp</item>
        <item name="android:textColorSecondary">@color/colorAccent</item>
        <item name="buttonStyle">@style/ButtonStyle</item>
    </style>

In the menifest file apply to your activities like

<activity
            android:name=".view.activities.MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/AppTheme" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!