java - 如何在robotium中获取textview的背景颜色

标签 java android css junit robotium

大家好,我正在尝试获取 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/

相关文章:

android - Espresso with Firebase - 异步加载

html - 如何在 html 上的实际屏幕框架上添加阴影?

jquery 逐一循环 li

java - 找不到与给定名称匹配的资源 : attr 'colorPrimaryDark'

java - Play Framework : Best practice for validation errors redirect

java - 安卓模拟器 : Installation error: INSTALL_FAILED_VERSION_DOWNGRADE

android - Gradle 打包时重复文件 - JodaTime 的 messages.properties

javascript - 将垂直文本与上面的条对齐

java - 是否可以从字符串表示构建 Apache HtmlEmail?

java - 从 Java 类型移回 MATLAB 类型