TextView,很常见的控件。关于文字大小的方法有:

android.widget.TextView#getTextSize  返回值的单位是PX

    /**
* @return the size (in pixels) of the default text size in this TextView.
*/
@ViewDebug.ExportedProperty(category = "text")
public float getTextSize() {
return mTextPaint.getTextSize();
}

  

android.widget.TextView#getScaledTextSize 返回值单位是SP

    /**
* @return the size (in scaled pixels) of thee default text size in this TextView.
* @hide
*/
@ViewDebug.ExportedProperty(category = "text")
public float getScaledTextSize() {
return mTextPaint.getTextSize() / mTextPaint.density;
}

  

android.widget.TextView#setTextSize(float) 参数的单位是SP

    /**
* Set the default text size to the given value, interpreted as "scaled
* pixel" units. This size is adjusted based on the current density and
* user font size preference.
*
* @param size The scaled pixel size.
*
* @attr ref android.R.styleable#TextView_textSize
*/
@android.view.RemotableViewMethod
public void setTextSize(float size) {
setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
}

  

android.widget.TextView#setTextSize(int, float) 参数的单位是两个,第一个是单位,第二个是数值

    /**
* Set the default text size to a given unit and value. See {@link
* TypedValue} for the possible dimension units.
*
* @param unit The desired dimension unit.
* @param size The desired size in the given units.
*
* @attr ref android.R.styleable#TextView_textSize
*/
public void setTextSize(int unit, float size) {
Context c = getContext();
Resources r; if (c == null)
r = Resources.getSystem();
else
r = c.getResources(); setRawTextSize(TypedValue.applyDimension(
unit, size, r.getDisplayMetrics()));
}

  

总结:

  • get方法,注意返回值的单位
  • set方法,注意参数的单位

补充

在自定义控件中使用自定义属性时,经常需要使用java代码获取在xml中定义的尺寸,相关有以下三个函数

  • getDimension()
  • getDimensionPixelOffset()
  • getDimensionPixelSize()

它们三个返回值的单位都是:PX

最新文章

  1. Apworks框架实战(三):单元测试与持续集成
  2. wf(六)
  3. Facebook 网络模拟工具 ATC部署及使用
  4. java多线程功力
  5. Perl Sort函数用法总结和使用实例
  6. 使用属性动画简单实现view飞入效果
  7. innodb锁之间的兼容性判断
  8. size_t和ssize_t
  9. 结构体page_cur_t
  10. Python队列服务 Python RQ Functions from the __main__ module cannot be processed by workers.
  11. Java 实现顺序查找
  12. Adb connect监听指定的主机和端口/Adb监听Visual Studio Emulator for Android模拟器
  13. malloc、calloc、realloc三者的差别
  14. [高级软件工程教学]团队Beta阶段成绩汇总
  15. 【EMV L2】CDA复合动态数据认证/应用密文生成
  16. 20175314薛勐 MyOD(课下作业,选做)
  17. Content-type详解
  18. 在VMware14上安装centos6.5
  19. Java SpringMVC框架学习(二)httpServeltRequest和Model传值的区别
  20. GridColumn (Column Layout and Auto Width)

热门文章

  1. cocos2d-x过程动作CCProgressTo示例学习笔记
  2. hdu2554-N对数的排列问题
  3. 系统变量写在.bash_profile和.bashrc的区别
  4. C#通过WebBrowser快速扒站思路积累大量着陆页列表
  5. JS中的内存泄漏
  6. MVC-03 控制器(1)
  7. LintCode-编辑距离
  8. 怪兽z主机标准版视频测试。
  9. Dijkstra算法详解
  10. 【水一发next_permutation】poj 1146——ID Codesm