Android: How to change app font size from code

拈花ヽ惹草 提交于 2019-12-04 03:28:40

问题


I have a requirement where I need to change the application font size based on user provided values. How can I do that? I have googled but it seems that this can only be done is from XML layouts.


回答1:


You can alter text size for textviews with

textview.setTextSize(int_value);

Is this what you meant?




回答2:


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




回答3:


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.




回答4:


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




回答5:


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

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



回答6:


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" />


来源:https://stackoverflow.com/questions/6162222/android-how-to-change-app-font-size-from-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!