Android 不同分辨率的文字大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12620343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:14:08  来源:igfitidea点击:

Text size with different resolution

androidandroid-layouttextresolution

提问by colymore

i am using a galaxy tab 1, 7inch hdpi and a galaxy tab plus, 7 inch hdpi but more resolution, and in my application, the text can be read fine in galaxy tab but in galaxy tab plus there are too much small. Im using sp in font size and layout-large. Any idea? Thanks

我正在使用星系标签 1、7 英寸 hdpi 和星系标签 plus、7 英寸 hdpi 但分辨率更高,在我的应用程序中,文本可以在星系标签中很好地阅读,但在星系标签中,文本太小了。我在字体大小和布局大中使用 sp。任何的想法?谢谢

回答by Dr Glass

This should be some help for you if you want to set size programmatically. Text will show in the same size on each device

如果您想以编程方式设置大小,这应该对您有所帮助。文本将在每个设备上以相同的大小显示

TextView text = new TextView(this);
text.setText("text");
text.setTextSize(16 * getResources().getDisplayMetrics().density);

回答by vasart

By hardware specifications Galaxy Tab 1 is MDPI device, but because it uses Android 2.x Samsung set it programmatically to use HDPI resources. So I can advice you to make following:

根据硬件规格,Galaxy Tab 1 是 MDPI 设备,但由于它使用 Android 2.x,三星以编程方式将其设置为使用 HDPI 资源。所以我可以建议你做以下事情:

  1. Create file dimens.xmlin valuesdirectory.
  2. Put there <dimen name="font_size">30sp</dimen>. This is default font size.
  3. Create file dimens.xmlin values-largedirectory.
  4. Put there <dimen name="font_size">20sp</dimen>. This is font size for galaxy tab 1.
  5. Create file dimens.xmlin values-sw600dpdirectory.
  6. Put there <dimen name="font_size">30sp</dimen>. This is font size for other tablets with Android 3.x and newer.
  7. In layout specify android:textSize="@dimens/font_size"
  1. dimens.xmlvalues目录中创建文件。
  2. 放在那里<dimen name="font_size">30sp</dimen>。这是默认字体大小。
  3. dimens.xmlvalues-large目录中创建文件。
  4. 放在那里<dimen name="font_size">20sp</dimen>。这是galaxy tab 1 的字体大小。
  5. dimens.xmlvalues-sw600dp目录中创建文件。
  6. 放在那里<dimen name="font_size">30sp</dimen>。这是其他装有 Android 3.x 及更新版本的平板电脑的字体大小。
  7. 在布局中指定 android:textSize="@dimens/font_size"

回答by SilentKiller

you just need to create different folders related to density or screen size like

您只需要创建与密度或屏幕尺寸相关的不同文件夹,例如

Option 1.

选项1。

values-large
values-small
values-normal

For more explanation check this link...

有关更多解释,请查看此链接...

Multiple Screen Support

多屏支持

Option 2.

选项 2。

mTextView.setTextSize(16 * getResources().getDisplayMetrics().density);

this will give TextSizedepending on density..

这将根据密度给出TextSize..

回答by SubbaReddy PolamReddy

try this:

尝试这个:

like this

像这样

android:textSize= "10sp"

sp

sp

Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.

Scale-independent Pixels - 这就像 dp 单位,但它也由用户的字体大小首选项缩放。建议您在指定字体大小时使用此单位,这样它们将根据屏幕密度和用户的喜好进行调整。

回答by Yogesh Somani

(1)I think using "dp" or "dip" for text is better than using "sp" because dp(density pixel) adjusts according to screen density. So text will look bigger on High-density devices and smaller on low-density devices.

(1)我认为对文本使用“dp”或“dip”比使用“sp”更好,因为dp(密度像素)根据屏幕密度进行调整。因此,文本在高密度设备上看起来更大,而在低密度设备上看起来更小。

(2) OR if you specifically want to use "sp" for text size, then you can detect the device density and then set the size of text accordingly:

(2) 或者,如果您特别想使用“sp”作为文本大小,那么您可以检测设备密度,然后相应地设置文本大小:

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    switch(displayMetrics.densityDpi){ 
        case DisplayMetrics.DENSITY_LOW: 
            //set text-size for low-density devices.
            break; 
        case DisplayMetrics.DENSITY_MEDIUM: 
            //set text-size for medium-density devices.
            break; 
        case DisplayMetrics.DENSITY_HIGH: 
            //set text-size for high-density devices.
            break; 
    } 

回答by Jin35

You can use any of thisdimensions, that market as based on the physical size of screen. Than texts on all devices will have the same physical size.

您可以使用任何此类尺寸,即基于屏幕物理尺寸的市场。所有设备上的文本将具有相同的物理大小。

回答by Rick

The below code adjusts font size scaling in a textView, using the User's system font size setting.

下面的代码使用用户的系统字体大小设置调整 textView 中的字体大小缩放。

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getRealSize(size);
int screenWidth = size.x;
float fontscale = getResources().getConfiguration().fontScale;
float textSize = (screenWidth/ fontscale)/10;
Log.e(TAG, "textSize is:  " + textSize);
yourTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,  textSize);

回答by yug

TextView headerTextView(String label){

TextView headerTextView(String label){

    TextView headerTextView = new TextView(this.context);
    float scale = context.getResources().getDisplayMetrics().density;
    if(scale > 2.0) {
    headerTextView.setWidth(210);
    headerTextView.setHeight(120);
    headerTextView.setTextSize(17);
    }
    else {
        headerTextView.setWidth(125);
        headerTextView.setHeight(60);
        headerTextView.setTextSize(21);
    }

回答by Birender Singh

In short, you can make use of scalable DPand scalable Fontapproach.

简而言之,您可以利用可扩展的 DP可扩展的 Font方法。

Android project structure inherently allows specifying different font size (SP) and margin values (DP) by defining them in dimens.xml files for supported screen sizes. But the problem that still remain is - what values to be used ? UX designers normally provide style guides for only one set (e.g. 360x640 DP screen size) and don't provide for all other supported screen sizes. So the problem to use right vlaues for other supported screen sizes still remain unanswered.

Android 项目结构本身允许通过在用于支持的屏幕尺寸的 dimens.xml 文件中定义不同的字体大小 (SP) 和边距值 (DP) 来指定它们。但仍然存在的问题是 - 要使用什么值?UX 设计师通常只提供一组样式指南(例如 360x640 DP 屏幕尺寸),而不提供所有其他支持的屏幕尺寸。因此,对于其他支持的屏幕尺寸使用正确的 vlaues 的问题仍然没有得到解答。

One good solution (per my understading) is to use readymade scalable dictionary of dimens.xml files, something similar to the dictionary of files provided by thislibrary. After adding these xml files to project, you just need to use the keys in your layouts, then Android automatically uses font/margin value from respective directory. Please refer herefor visual difference - with and without this approach.

一个好的解决方案(根据我的理解)是使用现成的可扩展的 dimens.xml 文件字典,类似于库提供的文件字典。将这些 xml 文件添加到项目后,您只需使用布局中的键,然后 Android 会自动使用相应目录中的字体/边距值。请参考这里的视觉差异 - 使用和不使用这种方法。