Button setTextAppearance(Context context, int resid) 已经被弃用。
setTextAppearance(int resid) - 仅适用于API级别23及以上版本。
应该使用什么替代方案呢?
Button setTextAppearance(Context context, int resid) 已经被弃用。
setTextAppearance(int resid) - 仅适用于API级别23及以上版本。
应该使用什么替代方案呢?
过时(Deprecated)意味着支持将来会被取消,但它仍然按预期工作。在旧的 API 上,由于新的 setTextAppearance(int resid)
仅在 API 级别 23 中发布,因此没有替代方案。
如果您想要长时间保持安全性,可以使用以下代码:
if (Build.VERSION.SDK_INT < 23) {
yourButton.setTextAppearance(context, resid);
} else {
yourButton.setTextAppearance(resid);
}
Build.VERSION>SDK_INT < 23
检查。将其放在一个位置,这样你将来容易删除它或进行更改。那么如何做呢?我将为yourButton
情况执行此操作。
Never use Button
or any other view provided by android just like that. I say this, because in the future you will need to tweak something and hence it's better to have your own MyButton
or something of that sort. So create MyButton
extends Button
.
Inside MyButton
, put the below code:
public void setTextAppearance(Context context, int resId) {
if (Build.VERSION.SDK_INT < 23) {
super.setTextAppearance(context, resId);
} else {
super.setTextAppearance(resId);
}
}
这样你就可以永远使用setTextAppearance
,而不需要担心检查BUILD版本。如果将来计划删除整个内容,则只需在一个地方进行重构
。 这是一些工作,但从长远来看,这将对您有所帮助,并减少一些维护噩梦。
TextView.setTextAppearance(int resId)
,从而导致对 super..
的双重调用吗? - guy_msetTextAppearance(int resId)
。没有对 super
的双重调用。 - HenrySTA(int)
,而API < 23只调用STA(context, int)
,对吗? :) 顺便问一下,系统什么时候会调用这些方法呢? - guy_m