简体   繁体   English

如何在Android中基于设备屏幕尺寸制作TextView的文本尺寸

[英]How to make TextView's text size based on device screen size in android

i have one app which can be runs on different screen size from 3.7 Inches to 6.0 inches(no plan for tablet now). 我有一个应用程序,可以在3.7英寸到6.0英寸的不同屏幕尺寸上运行(现在没有针对平板电脑的计划)。

now i have TextView which have 17dp for title and 14dp description . 现在我有TextView 的标题为 17dp,描述 14dp but when i run the app in small device (3.7-4.7 inches screen size device) the textview's text is big. 但是,当我在小型设备(3.7-4.7英寸屏幕大小的设备)中运行应用程序时,textview的文本很大。

But the same textview in higher 5.5 or 5.7 this can be small . 但是在5.5或5.7更高版本中相同的textview可能很小。

so how can i make TextView looks same for all device. 所以我怎样才能使TextView在所有设备上看起来都一样。

I had the same issue and I used this library and it did the thing for me. 我遇到了同样的问题,我使用了这个库,它为我做了事情。

The thing is you just have to replace sp with ssp in your xml after adding this library in your gradle and you'll be good to go. 问题是,只需在gradle中添加该库后,就必须在xml中用ssp替换sp,这样您就可以开始了。

Create multiple dimens.xml for your project, it will give size according to the display size 为您的项目创建多个dimens.xml,它将根据显示大小给出大小

You Have to create multiple values folder and have to create dimens.xml inside them. 您必须创建多个值文件夹,并且必须在其中创建dimens.xml。 A reference for those folders are: 这些文件夹的参考是:

values-sw720dp          10.1” tablet 1280x800 mdpi

values-sw600dp          7.0”  tablet 1024x600 mdpi

values-sw480dp          5.4”  480x854 mdpi 
values-sw480dp          5.1”  480x800 mdpi 

values-xxhdpi           5.5"  1080x1920 xxhdpi
values-xxxhdpi           5.5" 1440x2560 xxxhdpi

values-xhdpi            4.7”   1280x720 xhdpi 
values-xhdpi            4.65”  720x1280 xhdpi 

values-hdpi             4.0” 480x800 hdpi
values-hdpi             3.7” 480x854 hdpi

values-mdpi             3.2” 320x480 mdpi

values-ldpi             3.4” 240x432 ldpi
values-ldpi             3.3” 240x400 ldpi
values-ldpi             2.7” 240x320 ldpi

In android you are dealing with dp/sp values. 在android中,您正在处理dp / sp值。 Don't think of physical size. 不要认为身体大小。 There's this nifty plugin that lets you convert your sp/dp values to different buckets. 有一个漂亮的插件,可让您将sp / dp值转换为不同的存储桶。 You can add https://plugins.jetbrains.com/plugin/9349-dimenify 您可以添加https://plugins.jetbrains.com/plugin/9349-dimenify

As screen size is between 3.7 and 6.0 you should only create 1 additional dimens files like sw480 and use dp-s for TextView dimensions and sp for text size. 由于屏幕大小介于3.7和6.0之间,因此您仅应创建1个其他调暗文件(如sw480),并将dp-s用于TextView尺寸,将sp用于文本尺寸。 In default dimens file you should write sizes for devices with smallest screen sizes eg for 3.7', and comparably bigger dimens in sw480 file eg for 5.5' screens. 在默认的dimens文件中,您应该为屏幕尺寸最小的设备(例如3.7英寸)写入尺寸,在sw480文件中为5.5英寸的屏幕写入相对较大的尺寸。 Based on particular cases additional dimens files can be created like sw360. 根据特殊情况,可以创建其他调暗文件,如sw360。 If screen is less than 360 dp width,it will use the values from default dimens file, if 360dp <= screen size < 480dp then sw360 will be used. 如果屏幕小于360 dp宽度,它将使用默认的dimens文件中的值;如果360dp <=屏幕尺寸<480dp,则将使用sw360。 The same simple logic works for each created sw dimens file. 每个创建的sw dimens文件都使用相同的简单逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM