cap*_*ler 199 android textview text-size
如果我指定一个整数值来使用java代码更改TextView的某个文本大小,则该值将被解释为pixel(px).现在有谁知道如何在sp中分配它?
小智 516
http://developer.android.com/reference/android/widget/TextView.html#setTextSize%28int,%20float%29
例:
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 65);
Run Code Online (Sandbox Code Playgroud)
Dav*_*ebb 35
您可以使用DisplayMetrics对象来帮助使用scaledDensity属性在像素和缩放像素之间进行转换.
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
pixelSize = (int)scaledPixelSize * dm.scaledDensity;
Run Code Online (Sandbox Code Playgroud)
kli*_*mat 32
更清洁,更可重复使用的方法
dimens.xml在res/values/目录中的文件中定义文本大小:
</resources>
<dimen name="text_medium">14sp</dimen>
</resources>
Run Code Online (Sandbox Code Playgroud)
然后将其应用于TextView:
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.text_medium));
Run Code Online (Sandbox Code Playgroud)
rek*_*ire 19
基于以下源代码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()));
}
Run Code Online (Sandbox Code Playgroud)
我构建了这个函数来计算任何像素的demension:
int getPixels(int unit, float size) {
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
return (int)TypedValue.applyDimension(unit, size, metrics);
}
Run Code Online (Sandbox Code Playgroud)
单位是什么样的TypedValue.COMPLEX_UNIT_SP.
Zeu*_*ics 12
默认情况下为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.
Run Code Online (Sandbox Code Playgroud)
mac*_*Jun 11
谢谢@John Leehey和@PeterH:
desiredSp = getResources().getDimension(R.dimen.desired_sp);
density = getResources().getDisplayMetrics().density;
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, desiredSp / density);
Run Code Online (Sandbox Code Playgroud)
问题是如果你在dimen.xml中将R.dimen.desired_sp定义为25
pom*_*ber 10
当接受的答案不起作用时(例如处理Paint时),您可以使用:
float spTextSize = 12;
float textSize = spTextSize * getResources().getDisplayMetrics().scaledDensity;
textPaint.setTextSize(textSize);
Run Code Online (Sandbox Code Playgroud)
semeTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
context.getResources().getDimension(R.dimen.text_size_in_dp))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
94696 次 |
| 最近记录: |