@@ -28,8 +28,10 @@ public class BadgeView : TextView
28
28
private BadgePosition _position ;
29
29
30
30
public View Target { get ; private set ; }
31
- public int BadgeMarginH { get ; set ; }
32
- public int BadgeMarginV { get ; set ; }
31
+ private int _badgeMarginL ;
32
+ private int _badgeMarginR ;
33
+ private int _badgeMarginT ;
34
+ private int _badgeMarginB ;
33
35
34
36
public static int TextSizeDip { get ; set ; } = 11 ;
35
37
@@ -43,7 +45,7 @@ public BadgePosition Postion
43
45
{
44
46
return ;
45
47
}
46
-
48
+
47
49
_position = value ;
48
50
ApplyLayoutParams ( ) ;
49
51
}
@@ -66,6 +68,16 @@ public Color TextColor
66
68
set { SetTextColor ( value ) ; }
67
69
}
68
70
71
+ public void SetMargins ( float left , float top , float right , float bottom )
72
+ {
73
+ _badgeMarginL = DipToPixels ( left ) ;
74
+ _badgeMarginT = DipToPixels ( top ) ;
75
+ _badgeMarginR = DipToPixels ( right ) ;
76
+ _badgeMarginB = DipToPixels ( bottom ) ;
77
+
78
+ ApplyLayoutParams ( ) ;
79
+ }
80
+
69
81
public BadgeView ( Context context , View target ) : this ( context , null , Android . Resource . Attribute . TextViewStyle , target )
70
82
{
71
83
}
@@ -81,9 +93,11 @@ private void Init(Context context, View target)
81
93
Target = target ;
82
94
83
95
// apply defaults
84
- BadgeMarginH = DipToPixels ( DefaultHmarginDip ) ;
85
- BadgeMarginV = DipToPixels ( DefaultVmarginDip ) ;
86
-
96
+ _badgeMarginL = DipToPixels ( DefaultHmarginDip ) ;
97
+ _badgeMarginT = DipToPixels ( DefaultVmarginDip ) ;
98
+ _badgeMarginR = DipToPixels ( DefaultHmarginDip ) ;
99
+ _badgeMarginB = DipToPixels ( DefaultVmarginDip ) ;
100
+
87
101
Typeface = Typeface . DefaultBold ;
88
102
var paddingPixels = DipToPixels ( DefaultLrPaddingDip ) ;
89
103
SetPadding ( paddingPixels , 0 , paddingPixels , 0 ) ;
@@ -138,7 +152,7 @@ private void ApplyTo(View target)
138
152
139
153
group . SetClipChildren ( false ) ;
140
154
group . SetClipToPadding ( false ) ;
141
-
155
+
142
156
143
157
var container = new FrameLayout ( _context ) ;
144
158
var index = group . IndexOfChild ( target ) ;
@@ -200,47 +214,47 @@ private void ApplyLayoutParams()
200
214
{
201
215
case BadgePosition . PositionTopLeft :
202
216
layoutParameters . Gravity = GravityFlags . Left | GravityFlags . Top ;
203
- layoutParameters . SetMargins ( BadgeMarginH , BadgeMarginV , 0 , 0 ) ;
217
+ layoutParameters . SetMargins ( _badgeMarginL , _badgeMarginT , 0 , 0 ) ;
204
218
break ;
205
219
case BadgePosition . PositionTopRight :
206
220
layoutParameters . Gravity = GravityFlags . Right | GravityFlags . Top ;
207
- layoutParameters . SetMargins ( 0 , BadgeMarginV , BadgeMarginH , 0 ) ;
221
+ layoutParameters . SetMargins ( 0 , _badgeMarginT , _badgeMarginR , 0 ) ;
208
222
break ;
209
223
case BadgePosition . PositionBottomLeft :
210
224
layoutParameters . Gravity = GravityFlags . Left | GravityFlags . Bottom ;
211
- layoutParameters . SetMargins ( BadgeMarginH , 0 , 0 , BadgeMarginV ) ;
225
+ layoutParameters . SetMargins ( _badgeMarginL , 0 , 0 , _badgeMarginB ) ;
212
226
break ;
213
227
case BadgePosition . PositionBottomRight :
214
228
layoutParameters . Gravity = GravityFlags . Right | GravityFlags . Bottom ;
215
- layoutParameters . SetMargins ( 0 , 0 , BadgeMarginH , BadgeMarginV ) ;
229
+ layoutParameters . SetMargins ( 0 , 0 , _badgeMarginR , _badgeMarginB ) ;
216
230
break ;
217
231
case BadgePosition . PositionCenter :
218
232
layoutParameters . Gravity = GravityFlags . Center ;
219
233
layoutParameters . SetMargins ( 0 , 0 , 0 , 0 ) ;
220
234
break ;
221
235
case BadgePosition . PositionTopCenter :
222
236
layoutParameters . Gravity = GravityFlags . Center | GravityFlags . Top ;
223
- layoutParameters . SetMargins ( 0 , BadgeMarginV , 0 , 0 ) ;
237
+ layoutParameters . SetMargins ( 0 , _badgeMarginT , 0 , 0 ) ;
224
238
break ;
225
239
case BadgePosition . PositionBottomCenter :
226
240
layoutParameters . Gravity = GravityFlags . Center | GravityFlags . Bottom ;
227
- layoutParameters . SetMargins ( 0 , 0 , 0 , BadgeMarginV ) ;
241
+ layoutParameters . SetMargins ( 0 , 0 , 0 , _badgeMarginB ) ;
228
242
break ;
229
243
case BadgePosition . PositionLeftCenter :
230
244
layoutParameters . Gravity = GravityFlags . Left | GravityFlags . Center ;
231
- layoutParameters . SetMargins ( BadgeMarginH , 0 , 0 , 0 ) ;
245
+ layoutParameters . SetMargins ( _badgeMarginL , 0 , 0 , 0 ) ;
232
246
break ;
233
247
case BadgePosition . PositionRightCenter :
234
248
layoutParameters . Gravity = GravityFlags . Right | GravityFlags . Center ;
235
- layoutParameters . SetMargins ( 0 , 0 , BadgeMarginH , 0 ) ;
249
+ layoutParameters . SetMargins ( 0 , 0 , _badgeMarginR , 0 ) ;
236
250
break ;
237
251
}
238
252
239
253
LayoutParameters = layoutParameters ;
240
254
241
255
}
242
256
243
- private int DipToPixels ( int dip )
257
+ private int DipToPixels ( float dip )
244
258
{
245
259
return ( int ) TypedValue . ApplyDimension ( ComplexUnitType . Dip , dip , Resources . DisplayMetrics ) ;
246
260
}
0 commit comments