我创建一个自定义TextSwitcher如下如何从自定义视图中传递样式属性?
public class CustomTextSwitcher extends TextSwitcher {
private static final long SHOW_TEXT_ANIMATION_TIME = 100;
public CustomTextSwitcher(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
Animation in = AnimationUtils.loadAnimation(context, android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(context, android.R.anim.fade_out);
in.setDuration(SHOW_TEXT_ANIMATION_TIME);
out.setDuration(SHOW_TEXT_ANIMATION_TIME);
this.setInAnimation(in);
this.setOutAnimation(out);
}
public void setStyle(final int style) {
this.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
return new TextView(new ContextThemeWrapper(context, style),
null, 0);
}
});
}
}
这是一件好事,但我需要明确设置使用setStyle
功能如上声明,初始化后的风格。
我希望我不需要调用setStyle
只是申报我的风格在XML(如下代码所示),并获得int值通过构造attr
价值了,并沿该ViewFacory
发送,全部在init()
函数中完成。
<my.example.CustomTextSwitcher
android:id="@+id/search_list_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/recentSearchHeaderText" />
我怎么能做到这一点?