How to make font sizes in app to not get effected by changing Settings Font size

守給你的承諾、 提交于 2019-12-12 10:28:36

问题


I have my app running well as per design when the font size is at "Normal" on Settings->Display->FontSize.

When i change the font size to Small/Large entire content is mashed up. My app is content intensive and this is badly effecting the app.

Lets say, action bar title is like "Privacy and Settings". The title runs off the bounds/ellipsizes when font size is set to "Large" in System Settings. The listview contents look pathetic.

Is there a way to make sure that the change in Settings->Fonts doesn't effect our app?

Flipboard app does that.


回答1:


It is best to follow what Google recommends i.e to use 'sp' instead of 'dp'. Here is an updated solution which helped me - Solution

 //in base activity add this code.
public  void adjustFontScale( Configuration configuration) {

    configuration.fontScale = (float) 1.0;
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(metrics);
    metrics.scaledDensity = configuration.fontScale * metrics.density;
    getBaseContext().getResources().updateConfiguration(configuration, metrics);

}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    adjustFontScale( getResources().getConfiguration());
}  



回答2:


I finally got the solution. Google strictly recommends to use "sp" for fonts. However, if you do not want the text to be scaled based on user font settings, use "dp". The font size will not change in a given device if you go to settings->fonts. Your UI will remain just like you designed it for.

Thanks Aswin for your inputs. And thanks to Joseph Earl for answering question at Android sp vs dp texts - what would adjust the 'scale' and what is the philosophy of support




回答3:


Here is how you can do it but I don't recommend it.

float scale = getResources().getConfiguration().fontScale;
// This scale tells you what the font is scaled to.
// So if you want size 16, then set the size as 16/scale
float sizeNeeded = 16.0;
textView.setTextSize(sizeNeeded/scale);

Hope this helps.




回答4:


use dp or dip instead of sp in textSize



来源:https://stackoverflow.com/questions/21545906/how-to-make-font-sizes-in-app-to-not-get-effected-by-changing-settings-font-size

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