以编程方式创建具有特定样式的 ImageView

2024-03-28

我想以编程方式执行此操作:

<ImageView style="@style/TitleBarSeparator" />

Where TitleBarSeparator is:

<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));

尽管如此,我还是希望有一种方法来设置样式。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

以编程方式创建具有特定样式的 ImageView 的相关文章

随机推荐