[置顶] Xamarin android沉浸式状态栏
虽然关于android ”沉浸式“状态栏有很多博客介绍过,从小菜到大神无一例外。我第一次看到这种”沉浸“式的效果我也以为真的是这么叫,然而根本不是这么回事,完全是人云亦云。它真正的学名应该叫“透明状态栏”。不过lz管他怎么叫,但是必须得知道透明状态栏和沉浸式状态栏的区别。此文仅作用于小白理解android 透明状态栏,请android 大牛忽略此文,见笑了。
作为一名小白,android界面的还是应该要认识的
那么这篇文章的目的就是兼容android4.4和android5.0用两种方法来实现沉浸式状态栏(小白耐心看完,代码不多主要是图多)
第一种方法:设置状态栏透明化
我在qq空间随便get了一张手机截图,不知道是什么app的天气预报。这种方式利用的是将状态栏透明化(另一种方式状态栏设置颜色待会再说)
Google从Android kitkat(Android
4.4)开始(模仿IOS),给开发者提供了一套能透明的系统ui样式给状态栏和导航栏,所以要是实现这种浸入式导航栏,必须得android 4.4 以上的系统,而且android 4.4的系统和android 5.0的系统透明状态栏所实现的效果是不一样,什么区别?国际惯例上图吧(左4.4,右5.*)
虽然我们看到4.4 和5.* 的区别,这种效果的区别不是因为做法不同引起的,这里就介绍一下透明化式状态栏的效果
Activity.cs 直接用代码的方式,就这几行行代码,并没有去判断是否是5.*系统,仅仅只是设置状态栏为透明的。继承的主题是android自带的主题Theme.Light.NotitleBar。虽然你也可以写xml文件里面,但是在xamarin android 里面我发现设置状态栏透明属性无效,这的确是一个尴尬的地方,如果你知道怎么在xml文件里面设置状态栏的透明属性,欢迎评论。
[Activity(Label = "FirstActivity",MainLauncher =true,Theme = "@android:style/Theme.Light.NoTitleBar")]
public class FirstActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.First);
if (Build.VERSION.SdkInt >= Build.VERSION_CODES.Kitkat)
{
//透明状态栏
Window.AddFlags(WindowManagerFlags.TranslucentStatus);
//透明导航栏
Window.AddFlags(WindowManagerFlags.TranslucentNavigation);
}
}
}
布局文件是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="140dp"
android:textSize="24dp"
android:background="@color/colorPrimary"
android:text="你好,沉浸式状态栏"
android:textColor="@color/white" />
</LinearLayout>
这个布局文件要注意的是:fitsSystemWIndows属性,他是干嘛的呢?如果不设置为true的,可以看到效果是这样的
fitsSystemWindows属性的作用
Boolean internal attribute to adjust view layout based on system windows such as the status bar. If true, adjusts the padding of this view to leave space for the system windows. Will only take effect if this view is in a non-embedded
activity
简单的说就是:设置起bool值为true时就会自动调整view的padding属性,给system windows留出空间,实际效果: 当status bar为透明或半透明时(4.4以上),系统会设置view的paddingTop值为一个适合的值(status
bar的高度)让view的内容不被上拉到状态栏,当在不占据status bar的情况下(4.4以下)会设置paddingTop值为0(因为没有占据status bar所以不用留出空间)。
第二种方法:设置状态栏的颜色
这里我使用Toolbar来展示这个效果,当然你也可以自定义标题栏来做出这个效果来。先上图
效果还行,实现了兼容android4.4和android5.*。首先我们来看看这个布局的关键外面一个layout主体颜色设置成toolbar一样的背景颜色。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:toolbar="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
toolbar:logo="@drawable/menu"
android:subtitle="子标题"
toolbar:title="toolbar的标题"
android:textColor="@color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="toolbar的使用"
android:textColor="@color/white"/>
</android.support.v7.widget.Toolbar>
<!--内容布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示信息" />
</LinearLayout>
</LinearLayout>
Activity.cs如下。还有android4.4是不能设置状态栏颜色的,要判断android4.4和android5.*。值得注意的是添加的Flag不是方法一的那种Translucent,而是DrawsSystemBarBackgrounds 它才能修改状态栏的颜色(android5.*)
[Activity(Label = "FirstActivity123",MainLauncher =true,Theme = "@style/TranslucentTheme")]
public class FirstActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.First);
var toolBar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolBar);
SupportActionBar.SetDisplayShowTitleEnabled(false);//去掉标题
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{ //清除透明状态栏,使内容不再覆盖状态栏
Window.ClearFlags(WindowManagerFlags.TranslucentStatus);
Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
var Color = Resources.GetColor(Resource.Color.colorPrimary);
Window.SetStatusBarColor(Color);
//透明导航栏 部分手机导航栏不是虚拟的,比如小米的
Window.AddFlags(WindowManagerFlags.TranslucentNavigation);
Window.SetNavigationBarColor(Color);
}
if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat&&Build.VERSION.SdkInt <= BuildVersionCodes.Lollipop)
{
//状态栏透明
Window.AddFlags(WindowManagerFlags.TranslucentStatus);
//透明导航栏
Window.AddFlags(WindowManagerFlags.TranslucentNavigation);
}
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Drawable.base_toolbar_menu,menu);
return true;
}
}
Theme translucent 继承的v7兼容包主题Theme.AppCompat.Light
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="TranslucentTheme" parent="AppTheme.Base">
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
菜单文件就没必要贴出来了,主要是感受一下标题栏的颜色和状态栏设置成一样的。
小结:对于每个Activity都要去这样设置,既不简洁又麻烦,所以可以写个父类或者写个工具Class。
作者:张林
标题:Xamarin android沉浸式状态栏 原文地址:http://blog.csdn.net/kebi007/article/details/70215993
转载随意注明出处
有兴趣的可以关注一下我的微信公众号[dotNet全栈开发],分享一些编程相关的经典文章

[置顶] Xamarin android沉浸式状态栏的更多相关文章
- xamarin.android 沉浸式状态栏
public class SystemBarTintManager { /** * The default system bar tint color value. */ public static ...
- [置顶]
xamarin android Fragment实现底部导航栏
前段时间写了篇关于Fragment的文章,介绍了基础的概念,用静态和动态的方式加载Fragment Xamarin Android Fragment的两种加载方式.下面的这个例子介绍xamarin ...
- Android 沉浸式状态栏 实现方式二 ( 更简单 )
以前写过一个沉浸式状态栏 的实现方式 Android 沉浸式状态栏 实现方式一 现在有个更为简单的实现方式 . 相关链接 http://www.apkbus.com/forum.php?mod=vie ...
- android 沉浸式状态栏的实现
本文介绍一种简单的实现沉浸式状态栏的方法,要高于或等于api19才可以. 实现android沉浸式状态栏很简单,添加代码两步就可以搞定. 一.在activity中添加 getWindow().addF ...
- Android 沉浸式状态栏完美解决方案
现在搜索Android 沉浸式状态栏,真的是一堆一堆,写的特别多,但是真正用的舒服的真没有,在这里自己整理一下开发记录 注意,在使用这个步骤过程之前,请把之前设置的代码注释一下 把布局带有androi ...
- Android沉浸式状态栏(透明状态栏)最佳实现
Android沉浸式状态栏(透明状态栏)最佳实现 在Android4.4之前,我们的应用没法改变手机的状态栏颜色,当我们打开应用时,会出现上图中左侧的画面,在屏幕的顶部有一条黑色的状态栏,和应用的风格 ...
- 【Android实战】Android沉浸式状态栏实现(下)
之前的Android沉浸式状态栏实现并没有考虑软键盘的影响,接下来的内容将会针对这个问题给出解决方式,先看一下效果图 这个是一个留言板的效果图: 即弹出软键盘的时候并不会导致整个布局上移. 详细怎样实 ...
- Android 沉浸式状态栏攻略 让你的状态栏变色吧
转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/48649563: 本文出自:[张鸿洋的博客] 一.概述 近期注意到QQ新版使用了 ...
- Android沉浸式状态栏兼容4.4手机的实现
一.概述 最近注意到QQ新版使用了沉浸式状态栏,ok.先声明一下:本篇博客效果下图: 关于这个状态栏变色究竟叫「Immersive Mode」/「Translucent Bars」有兴趣能够去 为什么 ...
随机推荐
- 从今天起开始记录下在freecodecamp学习的一些tip吧(所有内容都在这个随笔的评论里面记录)
因为可能东西会很零碎 所以就放在随笔里吧 当需要在字符串中使用一个: " 或者 ' 时 可以通过在引号前面使用 反斜杠 (\) 来转义引号. var sampleStr = "Al ...
- C语言之猜数游戏
#include<stdio.h>#include<stdlib.h>#include<time.h>int main(){ srand(time(0)); int ...
- [特斯拉组件]ios高性能PageController
本文来自于腾讯Bugly公众号(weixinBugly),作者:sparrowchen,未经作者同意,请勿转载,原文地址: http://mp.weixin.qq.com/s/hBgvPBP12IQ1 ...
- #centos7 设置bond、bridge、vlan
#centos7 设置bond.bridge.vlan #centos7 设置bond.bridge.vlan # CentOS7中 nmcli命令由NetworkManager提供 # 可以用于设置 ...
- Handlebars 和 angularjs 之间的区别
handlebarsjs算不上框架,只是一种js模板引擎,是模板库,模板库的主要作用是:你想要生成某一大片有一定规律的界面,比如商品详情,不同商品之间差的只是名称,价格,图片,介绍这些,但是结构一样的 ...
- go语言常用开源库整理
框架 https://github.com/go-martini/martini 图形验证码 https://github.com/dchest/captcha ORM https://github. ...
- 有关BOM头的一些知识
在psr开发标准中,有一条是讲的,php只能使用无bom的utf8格式 . 那么这个bom是几个意思. 说一些理论内容 . 在UCS编码中有一个叫做"ZERO WIDTH NO-BREAK ...
- php综合运用技术
五.PHP综合应用 1.写出下列服务的用途和默认端口(新浪网技术部) ftp.ssh.http.telnet.https ftp:File Transfer Protocol,文件传输协议,是应用层的 ...
- react-native从开始趟的坑
好多天没更了..... 之前用的华为手机老人机真机调试的,最近几天换了小米,又遇上了坑... 跟之前所有手机一样打开开发者模式,开发者模式是(关于手机--版本号---一直点啊点--退出---辅助功能里 ...
- 初识java这个小姑娘(二)
妙解垃圾回收机制 周一,早高峰. 一段考验一个人耐力.智力.开车技术以及脾气的路. 我把车开进了一个没有红绿灯的丁字路口,然后就没有然后了. 来自三个方向的大车小车开始在不大的一块空间里开始互相斗智斗 ...