@@ -12,15 +12,15 @@ const R_ATTR_STATE_SELECTED = 0x010100a1;
12
12
const TITLE_TEXT_VIEW_ID = 16908310 ; // http://developer.android.com/reference/android/R.id.html#title
13
13
14
14
interface TabChangeListener {
15
- new ( owner : SegmentedBar ) : android . widget . TabHost . OnTabChangeListener ;
15
+ new ( owner : SegmentedBar ) : android . widget . TabHost . OnTabChangeListener ;
16
16
}
17
17
18
18
interface TabContentFactory {
19
- new ( owner : SegmentedBar ) : android . widget . TabHost . TabContentFactory ;
19
+ new ( owner : SegmentedBar ) : android . widget . TabHost . TabContentFactory ;
20
20
}
21
21
22
22
interface TabHost {
23
- new ( context : android . content . Context , attrs : android . util . AttributeSet ) : android . widget . TabHost ;
23
+ new ( context : android . content . Context , attrs : android . util . AttributeSet ) : android . widget . TabHost ;
24
24
}
25
25
26
26
let apiLevel : number ;
@@ -144,17 +144,18 @@ export class SegmentedBarItem extends SegmentedBarItemBase {
144
144
this . nativeViewProtected . setTypeface ( value instanceof Font ? value . getAndroidTypeface ( ) : value ) ;
145
145
}
146
146
147
- [ selectedBackgroundColorProperty . getDefault ] ( ) : android . graphics . drawable . Drawable . ConstantState {
147
+ [ selectedBackgroundColorProperty . getDefault ] ( ) : android . graphics . drawable . Drawable {
148
148
const viewGroup = < android . view . ViewGroup > this . nativeViewProtected . getParent ( ) ;
149
- return viewGroup . getBackground ( ) . getConstantState ( ) ;
149
+ return viewGroup . getBackground ( ) ;
150
150
}
151
- [ selectedBackgroundColorProperty . setNative ] ( value : Color | android . graphics . drawable . Drawable . ConstantState ) {
152
- const viewGroup = < android . view . ViewGroup > this . nativeViewProtected . getParent ( ) ;
151
+ [ selectedBackgroundColorProperty . setNative ] ( value : Color | android . graphics . drawable . Drawable ) {
152
+ const nativeView = this . nativeViewProtected ;
153
+ const viewGroup = < android . view . ViewGroup > nativeView . getParent ( ) ;
153
154
if ( value instanceof Color ) {
154
155
const color = value . android ;
155
156
const backgroundDrawable = viewGroup . getBackground ( ) ;
156
- if ( apiLevel > 21 && backgroundDrawable && typeof backgroundDrawable . setColorFilter === "function" ) {
157
- const newDrawable = backgroundDrawable . getConstantState ( ) . newDrawable ( ) ;
157
+ if ( apiLevel > 21 && backgroundDrawable ) {
158
+ const newDrawable = tryCloneDrawable ( backgroundDrawable , nativeView . getResources ( ) ) ;
158
159
newDrawable . setColorFilter ( color , android . graphics . PorterDuff . Mode . SRC_IN ) ;
159
160
org . nativescript . widgets . ViewHelper . setBackground ( viewGroup , newDrawable ) ;
160
161
} else {
@@ -164,15 +165,26 @@ export class SegmentedBarItem extends SegmentedBarItemBase {
164
165
arr [ 0 ] = R_ATTR_STATE_SELECTED ;
165
166
stateDrawable . addState ( arr , colorDrawable ) ;
166
167
stateDrawable . setBounds ( 0 , 15 , viewGroup . getRight ( ) , viewGroup . getBottom ( ) ) ;
167
-
168
168
org . nativescript . widgets . ViewHelper . setBackground ( viewGroup , stateDrawable ) ;
169
169
}
170
170
} else {
171
- org . nativescript . widgets . ViewHelper . setBackground ( viewGroup , value . newDrawable ( ) ) ;
171
+ const backgroundDrawable = tryCloneDrawable ( value , nativeView . getResources ( ) ) ;
172
+ org . nativescript . widgets . ViewHelper . setBackground ( viewGroup , backgroundDrawable ) ;
172
173
}
173
174
}
174
175
}
175
176
177
+ function tryCloneDrawable ( value : android . graphics . drawable . Drawable , resources : android . content . res . Resources ) : android . graphics . drawable . Drawable {
178
+ if ( value ) {
179
+ const constantState = value . getConstantState ( ) ;
180
+ if ( constantState ) {
181
+ return constantState . newDrawable ( resources ) ;
182
+ }
183
+ }
184
+
185
+ return value ;
186
+ }
187
+
176
188
export class SegmentedBar extends SegmentedBarBase {
177
189
nativeViewProtected : android . widget . TabHost ;
178
190
private _tabContentFactory : android . widget . TabHost . TabContentFactory ;
0 commit comments