Android TextView在不同屏幕尺寸下的大小

标签 android

我已经在Google上搜索了如何在不同屏幕尺寸下设置textview尺寸很长时间了,但我仍然没有得到解决方案。

我已经尝试将其设置为“dp”或“sp”,但在不同的屏幕尺寸下尺寸仍然不同。

我也尝试使用下面的代码来更改大小,但结果仍然错误。

final float scale = getResources().getDisplayMetrics().density;
textview = (int) (mysize * scale + 0.5f);

我可以使用其他方法来确保任何屏幕尺寸下的比例都是正确的吗?

谢谢。

最佳答案

我已经尝试将其设置为'dp'或'sp'。您必须使用 sp

您可以使用此逻辑

DisplayMetrics: A structure describing general information about a display, such as its size, density, and font scaling.

   DisplayMetrics metrics = getResources().getDisplayMetrics();

   int DeviceTotalWidth = metrics.widthPixels;
   int DeviceTotalHeight = metrics.heightPixels;

   TextViewObj.setTextSize(DeviceTotalWidth/8); // As per your Requirement 

您可以查看How to change TextView font size in android

关于Android TextView在不同屏幕尺寸下的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34781516/

相关文章:

Android如何将对象用户传递给 Activity fragment

php - ByetHost 服务器使用 JSON 字符串传递 html 值 "Checking your browser"

android - Kotlin使用Gson反序列化本地json文件

java - 我在 onAttach(Context) 中收到这个奇怪的错误

android - 从终端收到的Gradle buildConfigField值

android - deleteDirectory : java. lang.NoSuchMethodError: 没有虚拟方法 toPath

java - Android:CustomView 在应该对方向使用react时是静态的

java - 如何修复 Timertask 只运行一次?

android - 如果 SimpleCursorAdapter 关闭,则 ListView 为空()

android - Android 是否缓存在 xml 布局文件中加载的可绘制资源?