【问题标题】:how to get background color of textview in robotium如何在robotium中获取textview的背景颜色
【发布时间】:2014-10-02 07:59:43
【问题描述】:

大家好,我正在尝试获取 TextView 的背景颜色,但找不到。

我正在使用 robotsium 对我的聊天应用进行健全性自动化测试。

主要目标是找到天气文本视图气泡颜色为灰色或蓝色并进行断言。

    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

请帮助我,谢谢 :)

【问题讨论】:

  • 为什么不使用两条短信(发送和接收)并为它们断言

标签: java android css junit robotium


【解决方案1】:

经过一些研究,您可以使用

从文本视图中获取文本颜色
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?

希望对你有帮助,干杯。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    相关资源
    最近更新 更多