㈠ 為什麼國內android應用都不適配沉浸式狀態欄
簡單介紹
沉浸式是APP界面圖片延伸到狀態欄, 應用本身沉浸於狀態欄。當啟用該模式,應用程序的界面將占據整個屏幕,系統自動將隱藏系統的狀態欄和導航欄,讓應用程序內容可以在最大顯示範圍呈現。
常見問題
4.4及其以上都是可以實現沉浸式狀態欄效果的,5.0及其以上可以直接在主題中設置顏色,也可以調用Window類中的setStatusBarColor(int color)來實現,這兩種方式在5.0上都比較簡單。
圖片背景的頁面讓狀態欄透明及半透明。
㈡ 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中怎麼實現沉浸式狀態欄
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 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 怎麼做帶標題欄的沉浸式狀態欄
您可以嘗試著將AppTheme設置成沒有標題欄的樣式,在res->values->styles中進行修改:,希望能幫到您,謝謝。
㈥ 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>