问题
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