有一定的风格创建ImageView的编程
问题描述:
我想以编程方式做到这一点:有一定的风格创建ImageView的编程
<ImageView style="@style/TitleBarSeparator" />
其中TitleBarSeparator
是:
<style name="TitleBarSeparator">
<item name="android:layout_width">1px</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:background">@color/title_separator</item>
</style>
我已经试过:
new ImageView(getContext(), null, R.style.TitleBarSeparator);
但不起作用。 我想错误来自于传递null作为AttributeSet
,但我不完全确定。
答
我找到了解决这个问题的方法。
View v = new ImageView(getContext());
v.setLayoutParams(new LayoutParams(1, LayoutParams.FILL_PARENT));
v.setBackgroundColor(getContext().getResources().getColor(R.color.title_separator));
不过,我想有一个方法来设置样式。