android沉浸式状态栏实现
① android中怎么实现沉浸式状态栏
styles.xml设置如下:
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:actionModeBackground">@drawable/context_menu</item>
</style>
<style name="TranslucentTheme" parent="AppTheme.NoActionBar">
</style>
V21的styles.xml设置如下:
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:">true</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:statusBarColor">@color/colorPrimary</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:actionModeBackground">@drawable/context_menu</item>
</style>
<style name="TranslucentTheme" parent="AppTheme.NoActionBar">
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">false</item>
</style>
再在要显示的toolbar里加上属性:
android:fitsSystemWindows="true"
主题的属性设置为:
<style name="TranslucentTheme" parent="AppTheme.NoActionBar">
② Android QQ沉浸式状态栏效果是怎么实现的
首先是两个开启沉浸模式和关闭沉浸模式的函数
?
@SuppressLint("NewApi")
public static void hideSystemUI(View view) {
view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
@SuppressLint("NewApi")
public static void showSystemUI(View view) {
view.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
这些代码可以在google的开发者文档中找到,可以看这里Using Immersive Full-Screen Mode,上面的代码是在Android 4.4中才会生效,对应的Android版本兼容的判断请自行处理。
此外还需要一个辅助函数,用于获得状态栏高度,使用反射获得。
?
/**
* 获状态栏高度
*
* @param context 上下文
* @return 通知栏高度
*/
public int getStatusBarHeight(Context context) {
int statusBarHeight = 0;
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object obj = clazz.newInstance();
Field field = clazz.getField("status_bar_height");
int temp = Integer.parseInt(field.get(obj).toString());
statusBarHeight = context.getResources().getDimensionPixelSize(temp);
} catch (Exception e) {
e.printStackTrace();
}
return statusBarHeight;
}
点击hide按钮进入沉浸模式,也就是隐藏状态栏,隐藏状态栏的同时我们需要修改Toolbar的上内边距,否则会显得很难看,这里注册一个监听,当进入沉浸模式后我们改变Toolbar的上内边距
?
hide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View view = getWindow().getDecorView();
hideSystemUI(view);
mToolbar.set(new View.() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
mToolbar.setPadding(mToolbar.getPaddingLeft(), 0,mToolbar.getPaddingRight(), mToolbar.getPaddingBottom());
}
});
}
});
进入沉浸模式后,手指从屏幕顶部向下划,状态栏就出现了,过2秒左右它又会自动消失。
点击show按钮退出沉浸模式,同时Toolbar的内边距也要增加到状态栏的高度。
?
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View view = getWindow().getDecorView();
showSystemUI(view);
mToolbar.set(new View.() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
mToolbar.setPadding(mToolbar.getPaddingLeft(), getStatusBarHeight(MainActivity.this),mToolbar.getPaddingRight(), mToolbar.getPaddingBottom());
}
});
}
});
如果使用的是SystemBarTintManager这个类进行的状态栏的着色,除上方的操作外,还要在对应的监听里增加状态栏着色的禁止和启动的功能。
进入沉浸模式,要禁用
?
tintManager.setStatusBarTintEnabled(false);
退出沉浸模式,要启动
?
tintManager.setStatusBarTintEnabled(true);
如果你想更加平滑,则可以对padding的改成增加动画,具体动画效果自行添加。
③ android沉浸式状态栏透明怎么设置顶部高度
你这状态栏是变色龙状态栏,不是沉浸式的
这应该是沉浸式的状态栏吧,系统栏与actionbar颜色设为一致
我只想说去你妹的,老子只要自己的app的状态栏能和主题颜色一致就行了,定义那么多术语,让我等小白情以何堪?
吐槽归吐槽,但还是不得不去试着理解下这些术语怎么来的,引用这里的一段话:
沉浸式全屏模式
隐藏status bar(状态栏)使屏幕全屏,让Activity接收所有的(整个屏幕的)触摸事件。
透明化系统状态栏
透明化系统状态栏,使得布局侵入系统栏的后面,必须启用fitsSystemWindows属性来调整布局才不至于被系统栏覆盖。
因此,我就这样理解了:
沉浸式不就是隐藏状态栏嘛,状态栏不见了?这不就是app全屏模式嘛?wtf?
④ 如何实现Android沉浸式状态栏
styles.xml设置如下:
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:actionModeBackground">@drawable/context_menu</item>
</style>
<style name="TranslucentTheme" parent="AppTheme.NoActionBar">
</style>
V21的styles.xml设置如下:
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:">true</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:statusBarColor">@color/colorPrimary</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:actionModeBackground">@drawable/context_menu</item>
</style>
<style name="TranslucentTheme" parent="AppTheme.NoActionBar">
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">false</item>
</style>
再在要显示的toolbar里加上属性:
android:fitsSystemWindows="true"
主题的属性设置为:
<style name="TranslucentTheme" parent="AppTheme.NoActionBar">
⑤ android沉浸式状态栏都是白色怎么实现
在style文件里修改colorprimary的颜色就行了,不过要api21以上才能使用
⑥ android沉浸式状态栏怎么自适应
当启用该模式,应用程序的界面将占据整个屏幕,系统自动将隐藏系统的状态栏和导航栏,让应用程序内容可以在最大显示范围呈现,增加大屏体验,而当需要查看通知的时候只需要从顶部向下滑动就能呼出通知栏。
⑦ android studio怎么做沉浸式状态栏
答:沉浸式通知栏Android4.4以上才支持的新特性。4.3不支持。 具体实现方式如下: 1.新建个公共style,设置android:fitsSystemWindows=true true 2. 修改AndroidManifest.xml,让所有的activity样式默认设置为AppBaseTheme(*不同项目要灵活处理,笔...
⑧ 安卓开发中怎样设置沉浸式状态栏
这个特性是andorid4.4支持的,最少要api19才可以使用。下面介绍一下使用的方法,非常得简单:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明导航栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
//透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明导航栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
只要加入这两行代码,就可以实现沉浸式通知栏了。
给大家看看这个界面的布局:
<linearlayout android:background="#ffffff" android:cliptopadding="true" android:fitssystemwindows="true" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<textview android:background="#009959" android:layout_height="100dp" android:layout_width="match_parent"><button android:background="#ff669d/" android:layout_height="50dp" android:layout_width="100dp"></button></textview></linearlayout>
大家看红色的那部分,加入那两行以后,界面仍然会是沉浸式的,但状态栏那部分,就不会再重叠了,像加了padding一样,如下图:
大家看图,绿色的textView和红色的一个button都被下移了,状态栏是白色的,是背景linearLayout的颜色。很明显,这也不是我们想要的,我们希望状态栏和我们放在顶部的控件是同一个颜色,同时,控件内容也不和状态栏重复,其实,只要把那两行代码放到我们顶部的控件就可以了。代码如下:
<linearlayout android:background="#ffffff" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<textview android:background="#009959" android:cliptopadding="true" android:fitssystemwindows="true" android:layout_height="100dp" android:layout_width="match_parent" android:text="你好,请问你有男朋友吗/"><button android:background="#ff669d/" android:layout_height="50dp" android:layout_width="100dp"></button></textview></linearlayout>
就是那两行红色的代码,放在绿色的textView上,这样,就会是下面的效果:
这就是我们想要的了。
⑨ 安卓5.1怎么实现沉浸式状态栏
studio,中引入沉浸式兼容库
compile ‘com.readystatesoftware.systembartint:systembartint:1.0.3’
eclipse,可以导入相应的那个类。
第一类,兼容actionbar
第一步:设置activity主题android:theme=”@style/ActionBarTheme”
<style name="ActionBarTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
<item name="android:actionBarStyle">@style/ActionBarStyle</item>
</style>
<style name="ActionBarStyle" parent="android:Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">@color/actionbar_bg</item>
</style>
第二步:设置状态栏透明,然后设置状态栏沉浸的颜色
@TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
}
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
//设置沉浸的颜色 tintManager.setStatusBarTintResource(R.color.statusbar_bg);}
第三步:设置适应windows,在布局文件设置
android:fitsSystemWindows=”true”
如果不设置,应用的ui会顶上去,顶进system ui
ok
第二类 没有actionbar的activity
第一步,设置主题,android:theme=”@style/FullBleedTheme”
<style name="FullBleedTheme" parent="android:Theme.Holo.Light.NoActionBar">
<!-- API 14 theme customizations can go here. -->
</style>
<style name="FullBleedTheme" parent="android:Theme.Holo.Light.NoActionBar.TranslucentDecor">
<!-- API 19 theme customizations can go here. -->
</style>
或者
用toolbar只能设置Theme.AppCompat.NoActionBar主题
<style name="AppThemeToolbar" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#2196F3</item>
<item name="colorPrimaryDark">#2196F3</item>
<!--<item name="colorPrimaryDark">#1565C0</item>-->
<item name="colorAccent">#E91E63</item>
</style>
第二步:同上一个第二步。
设置状态栏透明+颜色
mTintManager = new SystemBarTintManager(this);
mTintManager.setStatusBarTintEnabled(true);
mTintManager.setNavigationBarTintEnabled(true); mTintManager.setStatusBarTintResource(R.color.statusbar_bg);
⑩ 如何实现android沉浸式状态栏
有些手机是强制改变通知栏颜色的,比如魅族,苹果。但是目前主要还是通过代码作出自己想要的效果。
Android 4.4之前,即使我们打开手机app,我们还总是能看到系统顶部那条黑乎乎的通知栏,这样会使得app稍显突兀。于是Android 4.4开始,便引入了Translucent System Bar的新特性,用于弥补系统通知栏突兀之处。
状态栏透明后,你可以选择设置其颜色或者显示背景图片。效果如下
<?xmlversion="1.0"encoding="utf-8"?><resources>
<!--red-->
<colorname="md_red_50_color_code">#fde0dc</color>
<colorname="md_red_100_color_code">#f9bdbb</color>
<colorname="md_red_200_color_code">#f69988</color>
<colorname="md_red_300_color_code">#f36c60</color>
<colorname="md_red_400_color_code">#e84e40</color>
<colorname="md_red_500_color_code">#e51c23</color>
<colorname="md_red_600_color_code">#dd191d</color>
<colorname="md_red_700_color_code">#d01716</color>
<colorname="md_red_800_color_code">#c41411</color>
<colorname="md_red_900_color_code">#b0120a</color>
<colorname="md_red_a100_color_code">#ff7997</color>
<colorname="md_red_a200_color_code">#ff5177</color>
<colorname="md_red_a400_color_code">#ff2d6f</color>
<colorname="md_red_a700_color_code">#e00032</color>
<!--pink-->
<colorname="md_pink_50_color_code">#fce4ec</color>
<colorname="md_pink_100_color_code">#f8bbd0</color>
<colorname="md_pink_200_color_code">#f48fb1</color>
<colorname="md_pink_300_color_code">#f06292</color>
<colorname="md_pink_400_color_code">#ec407a</color>
<colorname="md_pink_500_color_code">#e91e63</color>
<colorname="md_pink_600_color_code">#d81b60</color>
<colorname="md_pink_700_color_code">#c2185b</color>
<colorname="md_pink_800_color_code">#ad1457</color>
<colorname="md_pink_900_color_code">#880e4f</color>
<colorname="md_pink_a100_color_code">#ff80ab</color>
<colorname="md_pink_a200_color_code">#ff4081</color>
<colorname="md_pink_a400_color_code">#f50057</color>
<colorname="md_pink_a700_color_code">#c51162</color>
<!--deep_purple-->
<colorname="md_deep_purple_50_color_code">#ede7f6</color>
<colorname="md_deep_purple_100_color_code">#d1c4e9</color>
<colorname="md_deep_purple_200_color_code">#b39ddb</color>
<colorname="md_deep_purple_300_color_code">#9575cd</color>
<colorname="md_deep_purple_400_color_code">#7e57c2</color>
<colorname="md_deep_purple_500_color_code">#673ab7</color>
<colorname="md_deep_purple_600_color_code">#5e35b1</color>
<colorname="md_deep_purple_700_color_code">#512da8</color>
<colorname="md_deep_purple_800_color_code">#4527a0</color>
<colorname="md_deep_purple_900_color_code">#311b92</color>
<colorname="md_deep_purple_a100_color_code">#b388ff</color>
<colorname="md_deep_purple_a200_color_code">#7c4dff</color>
<colorname="md_deep_purple_a400_color_code">#651fff</color>
<colorname="md_deep_purple_a700_color_code">#6200ea</color>
<!--yellow-->
<colorname="md_yellow_50_color_code">#fffde7</color>
<colorname="md_yellow_100_color_code">#fff9c4</color>
<colorname="md_yellow_200_color_code">#fff59d</color>
<colorname="md_yellow_300_color_code">#fff176</color>
<colorname="md_yellow_400_color_code">#ffee58</color>
<colorname="md_yellow_500_color_code">#ffeb3b</color>
<colorname="md_yellow_600_color_code">#fdd835</color>
<colorname="md_yellow_700_color_code">#fbc02d</color>
<colorname="md_yellow_800_color_code">#f9a825</color>
<colorname="md_yellow_900_color_code">#f57f17</color>
<colorname="md_yellow_a100_color_code">#ffff8d</color>
<colorname="md_yellow_a200_color_code">#ffff00</color>
<colorname="md_yellow_a400_color_code">#ffea00</color>
<colorname="md_yellow_a700_color_code">#ffd600</color>
<!--orange-->
<colorname="md_orange_50_color_code">#fff3e0</color>
<colorname="md_orange_100_color_code">#ffe0b2</color>
<colorname="md_orange_200_color_code">#ffcc80</color>
<colorname="md_orange_300_color_code">#ffb74d</color>
<colorname="md_orange_400_color_code">#ffa726</color>
<colorname="md_orange_500_color_code">#ff9800</color>
<colorname="md_orange_600_color_code">#fb8c00</color>
<colorname="md_orange_700_color_code">#f57c00</color>
<colorname="md_orange_800_color_code">#ef6c00</color>
<colorname="md_orange_900_color_code">#e65100</color>
<colorname="md_orange_a100_color_code">#ffd180</color>
<colorname="md_orange_a200_color_code">#ffab40</color>
<colorname="md_orange_a400_color_code">#ff9100</color>
<colorname="md_orange_a700_color_code">#ff6d00</color>
<!--...............................-->
<!--grey-->
<colorname="md_grey_50_color_code">#fafafa</color>
<colorname="md_grey_100_color_code">#f5f5f5</color>
<colorname="md_grey_200_color_code">#eeeeee</color>
<colorname="md_grey_300_color_code">#e0e0e0</color>
<colorname="md_grey_400_color_code">#bdbdbd</color>
<colorname="md_grey_500_color_code">#9e9e9e</color>
<colorname="md_grey_600_color_code">#757575</color>
<colorname="md_grey_700_color_code">#616161</color>
<colorname="md_grey_800_color_code">#424242</color>
<colorname="md_grey_900_color_code">#212121</color>
<colorname="md_black_color_code">#000000</color>
<colorname="md_white_color_code">#ffffff</color>
<!--blue_grey-->
<colorname="md_blue_grey_50_color_code">#eceff1</color>
<colorname="md_blue_grey_100_color_code">#cfd8dc</color>
<colorname="md_blue_grey_200_color_code">#b0bec5</color>
<colorname="md_blue_grey_300_color_code">#90a4ae</color>
<colorname="md_blue_grey_400_color_code">#78909c</color>
<colorname="md_blue_grey_500_color_code">#607d8b</color>
<colorname="md_blue_grey_600_color_code">#546e7a</color>
<colorname="md_blue_grey_700_color_code">#455a64</color>
<colorname="md_blue_grey_800_color_code">#37474f</color>
<colorname="md_blue_grey_900_color_code">#263238</color>
<resources>