大家好,我正在尝试获取 TextView 的背景颜色,但找不到。
我正在使用 robotium 对我的聊天应用程序进行健全性自动化测试。
主要目标是找到天气 TextView 气泡颜色为灰色或蓝色并进行断言。
ArrayList<TextView> textViewList = solo.getCurrentViews(TextView.class);
for (TextView textview : textViewList )
{
if (textview != null && textview.isShown())
{
if(textview.getText().toString().equals(Message))
{
ColorDrawable drawable = (ColorDrawable)textview.getBackground();
int color= drawable.getColor();
//doing some assertion
}
}
}
这就是我想要得到颜色但有经验的东西
java.lang.ClassCastException: android.graphics.drawable.LayerDrawable cannot be cast to
android.graphics.drawable.ColorDrawable
请帮助我,谢谢 :)
最佳答案
经过一些研究,您可以使用
从 TextView 中获取文本颜色TextView text = (TextView) solo.getCurrentActivity().findViewById(R.id.TextViewColor);
text.getCurrentTextColor();
问题是返回的文本颜色是来自 R.java 的颜色,而不是您的 xml 中使用的颜色
更多内容请阅读这里:
colors.xml not generating the correct values in gen/R file?
希望对你有帮助,加油。
关于java - 如何在robotium中获取textview的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26156565/