setTextSize和getTextSize
当我们使用下面代码将textview1字体属性给textview时
textView.setTextSize(Float.parseFloat(String.valueOf(textView1.getTextSize())));
会发现文本字体变大问题
原因是getTextSize和setTextSize单位得到和设置的单位是不一样的
getTextSize返回值是以像素(px)为单位的,而setTextSize()是以sp为单位的。
解决方法:
将文本设置为px单位
textView1.setTextSize(TypedValue.COMPLEX_UNIT_PX,size)
setTextColor
设置颜色:
textView.setTextColor(ContextCompat.getColor(this,R.color.red))
问题:
以下代码不能将textview1的字体颜色设置给textview
textView.setTextColor(Integer.parseInt(String.valueOf(textView1.getTextColors())));
解决:
将textView.getTextColors()改为textView.getTextColors().getDefaultColor()
setBackgroundColor
textView.setBackgroundColor(ContextCompat.getColor(this,R.color.red))
获取textview1的背景颜色给texiview
Drawable drawable = textView1.getBackground();
ColorDrawable dra = (ColorDrawable) drawable;
textView.setBackgroundColor(Integer.parseInt(String.valueOf(dra.getColor());