意外投射到AppCompatButton:布局标签为Button [英] Unexpected cast to AppCompatButton: layout tag was Button

查看:540
本文介绍了意外投射到AppCompatButton:布局标签为Button的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 AppCompatButton 参考页:

当您在布局中使用Button时,将自动使用它.编写自定义视图时,您只需要手动使用此类.

This will automatically be used when you use Button in your layouts. You should only need to manually use this class when writing custom views.

我正在将普通的Button强制转换为AppCompatButton,以便可以使用setSupportBackgroundTintList方法:

I'm casting a normal Button to AppCompatButton, so that I can use setSupportBackgroundTintList method:

AppCompatButton button = (AppCompatButton) findViewById(R.id.normalButton);
button.setSupportBackgroundTintList(ColorStateList.valueOf(tintColor));

它的构建和运行没有任何问题,但是Android Studio 1.4在投放线上给了我烦人的红色亮点:

It builds and runs without any problem, but Android Studio 1.4 gives me the annoying red highlight on casting line:

意外投射到AppCompatButton:布局标签为Button

Unexpected cast to AppCompatButton: layout tag was Button

有什么想法吗?

推荐答案

这似乎是IDE类型检查中的错误-Button是AppCompatButton的直接祖先,因此可以将其强制转换为AppCompatButton.我相信您可以放心地这样称呼它:

It looks like a bug in the IDE type checking - Button is direct ancestor of AppCompatButton, so casting to AppCompatButton should be okay. I believe you can safely call it like this:

Button button = (Button) findViewById(R.id.normalButton);
((AppCompatButton)button).setSupportBackgroundTintList(ColorStateList.valueOf(tintColor));

或更好

((TintableBackgroundView)button).setSupportBackgroundTintList(ColorStateList.valueOf(tintColor));

如果使用Butterknife,一切都会正常进行,而不会发出任何警告:

If you use Butterknife, everything works as expected without any warning:

@Bind(R.id.normalButton)
AppCompatButton button;

这篇关于意外投射到AppCompatButton:布局标签为Button的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆