我正在将TextSwitcher与TextView工厂一起使用。我想将我在TextSwitcher上设置的样式传递给TextViews。
TextSwitcher没有3 arg构造函数。
是否可以从属性集中获取样式属性?
Xml
<com.my.TextSwitcher
style="@style/My.TextView.Style"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
爪哇
public class MyTextSwitcher extends TextSwitcher {
public MyTextSwitcher(Context context, AttributeSet attrs) {
super(context, attrs);
int style = attrs.getAttributeIntValue("", "style", 0); // I tried this to no avail
setFactory(new MyTextViewFactory(context, attrs, style));
}
private static class MyTextViewFactory implements ViewFactory {
private final Context context;
private final AttributeSet attrs;
private final int style;
public MyTextViewFactory(Context context, AttributeSet attrs, int style) {
this.context = context;
this.attrs = attrs;
this.style = style;
}
@Override
public View makeView() {
return new TextView(context, attrs, style);
}
}
}
使自己的int自定义属性通过样式的唯一答案是什么?我不能使用内置样式标签?
您可以使用以下方法检索style
属性(并将其传递给内部视图):
attrs.getStyleAttribute()
或其等价物(如文档所述):
getAttributeResourceValue(null, "style")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句