❶ 為什麼國內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);