I have some layouts and I want to adjust my textsize to the screen size. I used sp for all my sizes, but when I switch to my tablet my text still looks to small.
Is there a common scale formula or something to simply do this ?
try View.getHeight()
and View.getWidth()
and apply your logic accordingly.
OR
Use this :
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
OR
To get screen size in inches, try :
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(dm.widthPixels/dm.xdpi,2);
double y = Math.pow(dm.heightPixels/dm.ydpi,2);
double screenInches = Math.sqrt(x+y);
Log.d("debug","Screen inches : " + screenInches);
使用dip
而不是sp
但我确定默认的textsize取决于其容器,例如textView或Button
you can use
android:textSize="dip"
for eg. 10 dip,20 dip
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.