开发者

Change textSize based on screen res/density?

开发者 https://www.devze.com 2023-02-26 07:28 出处:网络
I have a grid of 18 buttons, apparently this is too big as my text is getting messed开发者_Go百科 up between different res/densities.

I have a grid of 18 buttons, apparently this is too big as my text is getting messed开发者_Go百科 up between different res/densities.

50sp looks right on 240 hdpi WVGA800, 30sp looks right on 120 ldpi QVGA and 160 mdpi HVGA. How can I adjust the sizes based on whatever screen is being used?


I'm 99.9% sure you can do it with different xml layouts... but if you're doing it programmatically you would do something like this:

DisplayMetrics dMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dMetrics);

//then run a switch

switch(dMetrics.densityDpi){

case DisplayMetrics.DENSITY_XHIGH:

    //set text size
    break;

case DisplayMetrics.DENSITY_HIGH:

    //set text size
    break;

case DisplayMetrics.DENSITY_MEDIUM:

    //set text size
    break;

case DisplayMetrics.DENSITY_LOW:

    //set text size
    break;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消