❶ 为什么国内android应用都不适配沉浸式状态栏
简单介绍
沉浸式是APP界面图片延伸到状态栏, 应用本身沉浸于状态栏。当启用该模式,应用程序的界面将占据整个屏幕,系统自动将隐藏系统的状态栏和导航栏,让应用程序内容可以在最大显示范围呈现。
常见问题
4.4及其以上都是可以实现沉浸式状态栏效果的,5.0及其以上可以直接在主题中设置颜色,也可以调用Window类中的setStatusBarColor(int color)来实现,这两种方式在5.0上都比较简单。
图片背景的页面让状态栏透明及半透明。
❷ android开发android 4.4以下版本能不能实现沉浸式
android4.4以后便可以使用,网上有现成的类,复制出来后在调用的位置设置状态栏的颜色,与背景一样即可,然后再在布局文件里加两个配置属性(自行网络),可解决所有手机适配的BUG,否则小米手机会出问题
❸ 安卓开发中怎样设置沉浸式状态栏
这个特性是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上,这样,就会是下面的效果:
这就是我们想要的了。
❹ 如何实现Android沉浸式状态栏
方法一:系统的方式沉浸式状态栏实现
1、//当系统版本为4.4或者4.4以上时可以使用沉浸式状态栏
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明导航栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
2、布局加入:
android:fitsSystemWindows="true"
android:clipToPadding="true"
方法二:实现思路,添加隐藏布局,然后我们动态的计算状态栏的高度,然后把这个高度设置成这个隐藏的布局的高度,便可以实现
/**
* 通过反射的方式获取状态栏高度
*
* @return
*/
private int getStatusBarHeight() {
try {
Class<?> c = Class.forName("com.android.internal.R$dimen");
Object obj = c.newInstance();
Field field = c.getField("status_bar_height");
int x = Integer.parseInt(field.get(obj).toString());
return getResources().getDimensionPixelSize(x);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
方法三、用的github上的第三方库
1.库地址:github.com/jgilfelt/SystemBarTint
2.添加依赖库:
compile ‘com.readystatesoftware.systembartint:systembartint:1.0.3’
3、 android:fitsSystemWindows="true"
android:clipToPadding="true
4、 SystemBarTintManager tintManager = new SystemBarTintManager(this);
// 激活状态栏
tintManager.setStatusBarTintEnabled(true);
// enable navigation bar tint 激活导航栏
tintManager.setNavigationBarTintEnabled(true);
//设置系统栏设置颜色
//tintManager.setTintColor(R.color.red);
//给状态栏设置颜色
tintManager.setStatusBarTintResource(R.color.mask_tags_1);
//Apply the specified drawable or color resource to the system navigation bar.
//给导航栏设置资源
tintManager.setNavigationBarTintResource(R.color.mask_tags_1);
❺ 如何实现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>
❻ android 沉浸式状态栏和透明状态栏的区别
注意!两种方法的区别:
第一种:为顶部栏跟随当前activity的布局文件的背景的颜色,使用方便,不过也有点问题就是,如果有底部虚拟导航键的话,导航键的背景跟顶部的颜色一样,比如:
第二种:是通过设置顶部栏的颜色来显示的,可以解决第一种的不足,比如:
第一种使用方法:
第一、首先在values、values-v19、values-v21文件夹下的styles.xml都设置一个 Translucent System Bar 风格的Theme,如下图:
values/style.xml:
<style name="TranslucentTheme" parent="AppTheme">
<!--在Android 4.4之前的版本上运行,直接跟随系统主题-->
</style>123
values-v19/style.xml:
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>1234
values-v21/style.xml:
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">true</item>
<!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
<item name="android:statusBarColor">@android:color/transparent</item>
</style>123456
第二、在清单文件中配置需要沉浸式状态栏的activity加入theme
<activity android:name=".ImageActivity" android:theme="@style/TranslucentTheme" />
<activity android:name=".ColorActivity" android:theme="@style/TranslucentTheme" />12
第三、在Activity的布局文件中的跟布局加入“android:fitsSystemWindows=”true””,但是,这里需要区分一下,就是背景是图片还是纯色:
1.当背景为图片时,布局可以这么写:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/imgs_bj"
android:fitsSystemWindows="true">
</RelativeLayout>12345678
效果:
2.当背景为纯色,我们需要对布局划分一下,标题布局与内容布局,先把根布局背景设置成标题布局的背景色,然后标题背景色可以不用设置直接使用根布局的背景色,最后内容布局背景色设置为白色
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary" //根布局背景设置成“标题布局”想要的颜色
android:fitsSystemWindows="true"
android:orientation="vertical">
<!--标题布局-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@color/color_31c27c">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="这是标题"
android:textColor="@android:color/white"
android:textSize="20sp" />
</RelativeLayout>
<!--内容布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" //内容区域背景设置成白色
android:gravity="center"
android:orientation="vertical">
<Button
android:layout_marginTop="120dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="显示信息"
android:onClick="showMsg"
/>
</LinearLayout>
</LinearLayout>
❼ android 沉浸式布局是什么意思
就是状态栏随着壁纸颜色匹配
❽ android studio怎么做沉浸式状态栏
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中怎么实现沉浸式状态栏
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">
❿ 安卓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);