在Android中获取默认文本大小(以像素为单位

时间:2013-02-27 04:46:27

标签: android layout fonts

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

2 个答案:

答案 0 :(得分:6)

获取textView的文本大小非常简单 这是代码

textView.getTextSize();

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

答案 1 :(得分:1)

只需更改此行代码

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

 TypedArray a = context.obtainStyledAttributes(typedValue.data, textSizeAttr);
相关问题