Android 如何使用java代码在sp值中分配文本大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2069810/
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 04:25:01  来源:igfitidea点击:

How to assign text size in sp value using java code

androidtextviewtext-size

提问by capecrawler

If I assign an integer value to change a certain text size of a TextViewusing java code, the value is interpreted as pixel (px).

如果我分配一个整数值来更改TextView使用 java 代码的某个文本大小,则该值将被解释为像素 ( px)。

Now does anyone know how to assign it in sp?

现在有人知道如何分配它sp吗?

回答by Dave Webb

You can use a DisplayMetricsobject to help convert between pixels and scaled pixels with the scaledDensityattribute.

您可以使用DisplayMetrics对象帮助在像素和具有scaledDensity属性的缩放像素之间进行转换。

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
pixelSize = (int)scaledPixelSize * dm.scaledDensity; 

回答by klimat

Cleaner and more reusable approach is

更清洁和更可重用的方法是

define text size in dimens.xmlfile inside res/values/directory:

dimens.xml目录内的文件中定义文本大小res/values/

</resources>
   <dimen name="text_medium">14sp</dimen>
</resources>

and then apply it to the TextView:

然后将其应用于TextView

textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.text_medium));

回答by rekire

Based on the the source code of setTextSize:

基于以下源代码setTextSize

public void setTextSize(int unit, float size) {
    Context c = getContext();
    Resources r;

    if (c == null)
        r = Resources.getSystem();
    else
        r = c.getResources();

    setRawTextSize(TypedValue.applyDimension(
        unit, size, r.getDisplayMetrics()));
}

I build this function for calulating any demension to pixels:

我构建了这个函数来计算像素的任何维度:

int getPixels(int unit, float size) {
    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    return (int)TypedValue.applyDimension(unit, size, metrics);
}

Where unit is something like TypedValue.COMPLEX_UNIT_SP.

其中单位是类似的东西TypedValue.COMPLEX_UNIT_SP

回答by Zeus Monolitics

By default setTextSize, without units work in SP (scales pixel)

默认情况下 setTextSize,无单位在 SP 中工作(缩放像素)

public void setTextSize (float size) 
Added in API level 1
Set the default text size to the given value, interpreted as "scaled pixel" units. This 
size is adjusted based on the current density and user font size preference.

回答by pomber

When the accepted answer doesn't work (for example when dealing with Paint) you can use:

当接受的答案不起作用时(例如在处理 Paint 时),您可以使用:

float spTextSize = 12;
float textSize = spTextSize * getResources().getDisplayMetrics().scaledDensity;
textPaint.setTextSize(textSize);

回答by macio.Jun

Thanks @John Leehey and @PeterH:

感谢@John Leehey 和@PeterH:

desiredSp = getResources().getDimension(R.dimen.desired_sp);
density = getResources().getDisplayMetrics().density;
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, desiredSp / density);

The thing is if you define R.dimen.desired_sp to 25 in your dimen.xml

问题是,如果您在 dimen.xml 中将 R.dimen.desired_sp 定义为 25

  1. On non-HD Device: desiredSp is still 25, density = 1
  2. On HD Device(like Nexus 7 2nd Generation): desiredSp becomes 50 ish, density = 2
  1. 在非高清设备上:desiredSp 仍为 25,密度 = 1
  2. 在高清设备(如 Nexus 7 2nd Generation)上:desiredSp 变为 50 ish,密度 = 2

回答by William Hu

semeTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 
                       context.getResources().getDimension(R.dimen.text_size_in_dp))

回答by Kamran Gasimov

This is code for the convert PXto SPformat. 100% Works

这是将PX格式转换为SP格式的代码。100% 作品

view.setTextSize(TypedValue.COMPLEX_UNIT_PX, 24);

回答by tsig

After trying all the solutions and none giving acceptable results (maybe because I was working on a device with default very large fonts), the following worked for me (COMPLEX_UNIT_DIP = Device Independent Pixels):

在尝试了所有解决方案并且没有给出可接受的结果(可能是因为我在使用默认非常大字体的设备上工作)之后,以下对我有用(COMPLEX_UNIT_DIP = Device Independent Pixels):

textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);