2010-07-07 34 views
4

我想以編程方式做到這一點:有一定的風格創建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,但我不完全確定。

回答

0

我找到了解決這個問題的方法。

View v = new ImageView(getContext()); 
v.setLayoutParams(new LayoutParams(1, LayoutParams.FILL_PARENT)); 
v.setBackgroundColor(getContext().getResources().getColor(R.color.title_separator)); 

不過,我想有一個方法來設置樣式。