4

我需要知道当前默认 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()
4

2 回答 2

6

获取 textView 的文本大小非常简单,代码如下

textView.getTextSize();

返回 此 TextView 中默认文本大小的大小(以像素为单位)

于 2013-02-27T05:17:18.683 回答
1

只需更改这行代码

TypedArray a = context.obtainStyledAttributes((AttributeSet) typedValue.string, textSizeAttr);

 TypedArray a = context.obtainStyledAttributes(typedValue.data, textSizeAttr);
于 2013-02-27T05:44:27.333 回答