我需要知道当前默认 Android 字体的像素大小。我在这里复制了一个非常相似的问题中列出的方法。但是,我总是得到-1,而不是以像素为单位。任何想法我做错了什么?
这是代码:
Context context = getActivity();
TypedValue typedValue = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.textAppearance, typedValue, true);
int[] textSizeAttr = new int[] { android.R.attr.textSize };
TypedArray a = context.obtainStyledAttributes((AttributeSet) typedValue.string, textSizeAttr);
textSize = a.getDimensionPixelSize(0, -1);
Log.e("Metrics", "text size in dp = " + String.valueOf(textSize));
a.recycle()