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

标签 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()

最佳答案

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

textView.getTextSize();

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

关于android - 在 Android 中获取以像素为单位的默认文本大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15104409/

相关文章:

Android-提供替代资源不起作用?

fonts - 如果我使用Google网络字体,Google可以跟踪我吗?

css - 是否可以添加 Myriad pro 字体,就像离线 Web 应用程序的自定义字体一样

ios - 在 iOS 应用程序中安装特定字体时遇到问题 - 不兼容的 TTF?

android - 重复使用 "findViewById"还是存储View?

android - 如何获取安卓源

android - ADT : Java heap space 问题

android - 在 eclipse 中使用 maven android 原型(prototype)

git - 需要新项目的 git repo 布局建议

java - 如何指定希望工具栏中的元素居中?