我需要从TextView获取当前文本颜色,然后将此值分配给TextView.setTextColor()。 但我获得了一个大整数-1979711488138,如何从中获取颜色?
Integer intColor = -1979711488138;
String hexColor = "#" + Integer.toHexString(intColor).substring(2);
或者
String hexColor = String.format("#%06X", (0xFFFFFF & intColor));
textView1
到 textView
的颜色设置为红色,那么您可以这样做:textView.setTextColor(textView1.getCurrentTextColor());
你无法将该数字放入int中,我得到的是-1979711488,即#8A000000,即带有138个alpha的黑色。您可以像这样获取颜色的所有部分:
int color = getCurrentTextColor();
int a = Color.alpha(color);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
为什么默认文本颜色不是一种纯色,而是带有 alpha 值的黑色,这对系统来说更加昂贵,这真的很奇怪。
public class MainActivity extends Activity
{
private TextView txtViewIpLable,textView1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
textView1.setTextColor(txtViewIpLable.getTextColors());
}
private void init()
{
// TODO Auto-generated method stub
txtViewIpLable = (TextView) findViewById(R.id.txtViewIpLable);
textView1 = (TextView) findViewById(R.id.textView1);
}
}
tv.setTextColor(tv2.getTextColors());
- Rustam