我需要知道当前默认 Android 字体的像素大小。我复制了一个非常相似的问题中列出的方法 here .但是,我得到的不是像素大小,而是 -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()
最佳答案
获取textView的文字大小非常简单 这是代码
textView.getTextSize();
返回 此 TextView 中默认文本大小的大小(以像素为单位)
关于android - 在 Android 中获取以像素为单位的默认文本大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15104409/