苹果上的ui基本上都是这个效果,然而android机上的顶部状态栏总是和app的主题颜色不搭。还好如今的api19以上的版本,我们也能做出这样的效果。
第一步:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
// 需要setcontentview之前调用 private void settranslucentstatus() { if (build.version.sdk_int >= build.version_codes.kitkat) { // 透明状态栏 getwindow().addflags( windowmanager.layoutparams.flag_translucent_status); // 透明导航栏 getwindow().addflags( windowmanager.layoutparams.flag_translucent_navigation); systemstatusmanager tintmanager = new systemstatusmanager( this ); tintmanager.setstatusbartintenabled( true ); // 设置状态栏的颜色 tintmanager.setstatusbartintresource(r.color.theme_color); getwindow().getdecorview().setfitssystemwindows( true ); } } |
第二步:
为xml 的根布局添加android:fitssystemwindows=”true” 属性
第三步:systemstatusmanager
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
import android.annotation.suppresslint; import android.annotation.targetapi; import android.app.activity; import android.content.context; import android.content.res.configuration; import android.content.res.resources; import android.content.res.typedarray; import android.graphics.drawable.drawable; import android.os.build; import android.util.displaymetrics; import android.util.typedvalue; import android.view.gravity; import android.view.view; import android.view.viewconfiguration; import android.view.viewgroup; import android.view.window; import android.view.windowmanager; import android.widget.framelayout.layoutparams; import java.lang.reflect.method; @suppresswarnings ({ “unchecked”, “rawtypes” }) public class systemstatusmanager { static { if (build.version.sdk_int >= build.version_codes.kitkat) { try { class c = class .forname(“android.os.systemproperties”); method m = c.getdeclaredmethod(“get”, string. class ); m.setaccessible( true ); snavbaroverride = (string) m.invoke( null , “qemu.hw.mainkeys”); } catch (throwable e) { snavbaroverride = null ; } } } /** * the default system bar tint color value. */ public static final int default_tint_color = 0x99000000 ; private static string snavbaroverride; private final systembarconfig mconfig; private boolean mstatusbaravailable; private boolean mnavbaravailable; private boolean mstatusbartintenabled; private boolean mnavbartintenabled; private view mstatusbartintview; private view mnavbartintview; /** * constructor. call this in the host activity oncreate method after its * content view has been set. you should always create new instances when * the host activity is recreated. * * @param activity the host activity. */ @targetapi ( 19 ) public systemstatusmanager(activity activity) { window win = activity.getwindow(); viewgroup decorviewgroup = (viewgroup) win.getdecorview(); if (build.version.sdk_int >= build.version_codes.kitkat) { // check theme attrs int [] attrs = {android.r.attr.windowtranslucentstatus, android.r.attr.windowtranslucentnavigation}; typedarray a = activity.obtainstyledattributes(attrs); try { mstatusbaravailable = a.getboolean( 0 , false ); mnavbaravailable = a.getboolean( 1 , false ); } finally { a.recycle(); } // check window flags windowmanager.layoutparams winparams = win.getattributes(); int bits = windowmanager.layoutparams.flag_translucent_status; if ((winparams.flags & bits) != 0 ) { mstatusbaravailable = true ; } bits = windowmanager.layoutparams.flag_translucent_navigation; if ((winparams.flags & bits) != 0 ) { mnavbaravailable = true ; } } mconfig = new systembarconfig(activity, mstatusbaravailable, mnavbaravailable); // device might not have virtual navigation keys if (!mconfig.hasnavigtionbar()) { mnavbaravailable = false ; } if (mstatusbaravailable) { setupstatusbarview(activity, decorviewgroup); } if (mnavbaravailable) { setupnavbarview(activity, decorviewgroup); } } /** * enable tinting of the system status bar. * * if the platform is running jelly bean or earlier, or translucent system * ui modes have not been enabled in either the theme or via window flags, * then this method does nothing. * * @param enabled true to enable tinting, false to disable it (default). */ public void setstatusbartintenabled( boolean enabled) { mstatusbartintenabled = enabled; if (mstatusbaravailable) { mstatusbartintview.setvisibility(enabled ? view.visible : view.gone); } } /** * enable tinting of the system navigation bar. * * if the platform does not have soft navigation keys, is running jelly bean * or earlier, or translucent system ui modes have not been enabled in either * the theme or via window flags, then this method does nothing. * * @param enabled true to enable tinting, false to disable it (default). */ public void setnavigationbartintenabled( boolean enabled) { mnavbartintenabled = enabled; if (mnavbaravailable) { mnavbartintview.setvisibility(enabled ? view.visible : view.gone); } } /** * apply the specified color tint to all system ui bars. * * @param color the color of the background tint. */ public void settintcolor( int color) { setstatusbartintcolor(color); setnavigationbartintcolor(color); } /** * apply the specified drawable or color resource to all system ui bars. * * @param res the identifier of the resource. */ public void settintresource( int res) { setstatusbartintresource(res); setnavigationbartintresource(res); } /** * apply the specified drawable to all system ui bars. * * @param drawable the drawable to use as the background, or null to remove it. */ public void settintdrawable(drawable drawable) { setstatusbartintdrawable(drawable); setnavigationbartintdrawable(drawable); } /** * apply the specified alpha to all system ui bars. * * @param alpha the alpha to use */ public void settintalpha( float alpha) { setstatusbaralpha(alpha); setnavigationbaralpha(alpha); } /** * apply the specified color tint to the system status bar. * * @param color the color of the background tint. */ public void setstatusbartintcolor( int color) { if (mstatusbaravailable) { mstatusbartintview.setbackgroundcolor(color); } } /** * apply the specified drawable or color resource to the system status bar. * * @param res the identifier of the resource. */ public void setstatusbartintresource( int res) { if (mstatusbaravailable) { mstatusbartintview.setbackgroundresource(res); } } /** * apply the specified drawable to the system status bar. * * @param drawable the drawable to use as the background, or null to remove it. */ @suppresswarnings ( "deprecation" ) public void setstatusbartintdrawable(drawable drawable) { if (mstatusbaravailable) { mstatusbartintview.setbackgrounddrawable(drawable); } } /** * apply the specified alpha to the system status bar. * * @param alpha the alpha to use */ @targetapi ( 11 ) public void setstatusbaralpha( float alpha) { if (mstatusbaravailable && build.version.sdk_int >= build.version_codes.honeycomb) { mstatusbartintview.setalpha(alpha); } } /** * apply the specified color tint to the system navigation bar. * * @param color the color of the background tint. */ public void setnavigationbartintcolor( int color) { if (mnavbaravailable) { mnavbartintview.setbackgroundcolor(color); } } /** * apply the specified drawable or color resource to the system navigation bar. * * @param res the identifier of the resource. */ public void setnavigationbartintresource( int res) { if (mnavbaravailable) { mnavbartintview.setbackgroundresource(res); } } /** * apply the specified drawable to the system navigation bar. * * @param drawable the drawable to use as the background, or null to remove it. */ @suppresswarnings ( "deprecation" ) public void setnavigationbartintdrawable(drawable drawable) { if (mnavbaravailable) { mnavbartintview.setbackgrounddrawable(drawable); } } /** * apply the specified alpha to the system navigation bar. * * @param alpha the alpha to use */ @targetapi ( 11 ) public void setnavigationbaralpha( float alpha) { if (mnavbaravailable && build.version.sdk_int >= build.version_codes.honeycomb) { mnavbartintview.setalpha(alpha); } } /** * get the system bar configuration. * * @return the system bar configuration for the current device configuration. */ public systembarconfig getconfig() { return mconfig; } /** * is tinting enabled for the system status bar? * * @return true if enabled, false otherwise. */ public boolean isstatusbartintenabled() { return mstatusbartintenabled; } /** * is tinting enabled for the system navigation bar? * * @return true if enabled, false otherwise. */ public boolean isnavbartintenabled() { return mnavbartintenabled; } private void setupstatusbarview(context context, viewgroup decorviewgroup) { mstatusbartintview = new view(context); layoutparams params = new layoutparams(layoutparams.match_parent, mconfig.getstatusbarheight()); params.gravity = gravity.top; if (mnavbaravailable && !mconfig.isnavigationatbottom()) { params.rightmargin = mconfig.getnavigationbarwidth(); } mstatusbartintview.setlayoutparams(params); mstatusbartintview.setbackgroundcolor(default_tint_color); mstatusbartintview.setvisibility(view.gone); decorviewgroup.addview(mstatusbartintview); } private void setupnavbarview(context context, viewgroup decorviewgroup) { mnavbartintview = new view(context); layoutparams params; if (mconfig.isnavigationatbottom()) { params = new layoutparams(layoutparams.match_parent, mconfig.getnavigationbarheight()); params.gravity = gravity.bottom; } else { params = new layoutparams(mconfig.getnavigationbarwidth(), layoutparams.match_parent); params.gravity = gravity.right; } mnavbartintview.setlayoutparams(params); mnavbartintview.setbackgroundcolor(default_tint_color); mnavbartintview.setvisibility(view.gone); decorviewgroup.addview(mnavbartintview); } /** * class which describes system bar sizing and other characteristics for the current * device configuration. * */ public static class systembarconfig { private static final string status_bar_height_res_name = "status_bar_height" ; private static final string nav_bar_height_res_name = "navigation_bar_height" ; private static final string nav_bar_height_landscape_res_name = "navigation_bar_height_landscape" ; private static final string nav_bar_width_res_name = "navigation_bar_width" ; private static final string show_nav_bar_res_name = "config_shownavigationbar" ; private final boolean mtranslucentstatusbar; private final boolean mtranslucentnavbar; private final int mstatusbarheight; private final int mactionbarheight; private final boolean mhasnavigationbar; private final int mnavigationbarheight; private final int mnavigationbarwidth; private final boolean minportrait; private final float msmallestwidthdp; private systembarconfig(activity activity, boolean translucentstatusbar, boolean traslucentnavbar) { resources res = activity.getresources(); minportrait = (res.getconfiguration().orientation == configuration.orientation_portrait); msmallestwidthdp = getsmallestwidthdp(activity); mstatusbarheight = getinternaldimensionsize(res, status_bar_height_res_name); mactionbarheight = getactionbarheight(activity); mnavigationbarheight = getnavigationbarheight(activity); mnavigationbarwidth = getnavigationbarwidth(activity); mhasnavigationbar = (mnavigationbarheight > 0 ); mtranslucentstatusbar = translucentstatusbar; mtranslucentnavbar = traslucentnavbar; } @targetapi ( 14 ) private int getactionbarheight(context context) { int result = 0 ; if (build.version.sdk_int >= build.version_codes.ice_cream_sandwich) { typedvalue tv = new typedvalue(); context.gettheme().resolveattribute(android.r.attr.actionbarsize, tv, true ); result = typedvalue.complextodimensionpixelsize(tv.data, context.getresources().getdisplaymetrics()); } return result; } @targetapi ( 14 ) private int getnavigationbarheight(context context) { resources res = context.getresources(); int result = 0 ; if (build.version.sdk_int >= build.version_codes.ice_cream_sandwich) { if (hasnavbar(context)) { string key; if (minportrait) { key = nav_bar_height_res_name; } else { key = nav_bar_height_landscape_res_name; } return getinternaldimensionsize(res, key); } } return result; } @targetapi ( 14 ) private int getnavigationbarwidth(context context) { resources res = context.getresources(); int result = 0 ; if (build.version.sdk_int >= build.version_codes.ice_cream_sandwich) { if (hasnavbar(context)) { return getinternaldimensionsize(res, nav_bar_width_res_name); } } return result; } @targetapi ( 14 ) private boolean hasnavbar(context context) { resources res = context.getresources(); int resourceid = res.getidentifier(show_nav_bar_res_name, "bool" , "android" ); if (resourceid != 0 ) { boolean hasnav = res.getboolean(resourceid); // check override flag (see static block) if ( "1" .equals(snavbaroverride)) { hasnav = false ; } else if ( "0" .equals(snavbaroverride)) { hasnav = true ; } return hasnav; } else { // fallback return !viewconfiguration.get(context).haspermanentmenukey(); } } private int getinternaldimensionsize(resources res, string key) { int result = 0 ; int resourceid = res.getidentifier(key, "dimen" , "android" ); if (resourceid > 0 ) { result = res.getdimensionpixelsize(resourceid); } return result; } @suppresslint ( "newapi" ) private float getsmallestwidthdp(activity activity) { displaymetrics metrics = new displaymetrics(); if (build.version.sdk_int >= build.version_codes.jelly_bean) { activity.getwindowmanager().getdefaultdisplay().getrealmetrics(metrics); } else { activity.getwindowmanager().getdefaultdisplay().getmetrics(metrics); } float widthdp = metrics.widthpixels / metrics.density; float heightdp = metrics.heightpixels / metrics.density; return math.min(widthdp, heightdp); } /** * should a navigation bar appear at the bottom of the screen in the current * device configuration? a navigation bar may appear on the right side of * the screen in certain configurations. * * @return true if navigation should appear at the bottom of the screen, false otherwise. */ public boolean isnavigationatbottom() { return (msmallestwidthdp >= 600 || minportrait); } /** * get the height of the system status bar. * * @return the height of the status bar (in pixels). */ public int getstatusbarheight() { return mstatusbarheight; } /** * get the height of the action bar. * * @return the height of the action bar (in pixels). */ public int getactionbarheight() { return mactionbarheight; } /** * does this device have a system navigation bar? * * @return true if this device uses soft key navigation, false otherwise. */ public boolean hasnavigtionbar() { return mhasnavigationbar; } /** * get the height of the system navigation bar. * * @return the height of the navigation bar (in pixels). if the device does not have * soft navigation keys, this will always return 0. */ public int getnavigationbarheight() { return mnavigationbarheight; } /** * get the width of the system navigation bar when it is placed vertically on the screen. * * @return the width of the navigation bar (in pixels). if the device does not have * soft navigation keys, this will always return 0. */ public int getnavigationbarwidth() { return mnavigationbarwidth; } /** * get the layout inset for any system ui that appears at the top of the screen. * * @param withactionbar true to include the height of the action bar, false otherwise. * @return the layout inset (in pixels). */ public int getpixelinsettop( boolean withactionbar) { return (mtranslucentstatusbar ? mstatusbarheight : 0 ) + (withactionbar ? mactionbarheight : 0 ); } /** * get the layout inset for any system ui that appears at the bottom of the screen. * * @return the layout inset (in pixels). */ public int getpixelinsetbottom() { if (mtranslucentnavbar && isnavigationatbottom()) { return mnavigationbarheight; } else { return 0 ; } } /** * get the layout inset for any system ui that appears at the right of the screen. * * @return the layout inset (in pixels). */ public int getpixelinsetright() { if (mtranslucentnavbar && !isnavigationatbottom()) { return mnavigationbarwidth; } else { return 0 ; } } } } |
以上就是本文的全部内容,希望大家喜欢。