溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android 實現背景圖和狀態欄融合方法

發布時間:2020-08-19 19:14:40 來源:腳本之家 閱讀:217 作者:童童童童思宇 欄目:移動開發

我們先看一下代碼:

public class MainActivity extends AppCompatActivity {
  ...
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= 21){
      View decorView = getWindow().getDecorView();
      decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
      getWindow().setStatusBarColor(Color.TRANSPARENT);
    }
    setContentView(R.layout.activity_main);
      ...
  }

由于這個功能是Android5.0及以上的系統才支持的,因此我們先在代碼中做一個系統版本號的判斷,只有當版本號大于或等于21的時候,也就是5.0及以上系統時才會執行后面的代碼。

接著我們調用了getWindow().getDecorView()方法拿到當前活動的DecorView,再調用它的setSystemUiVisibility()方法來改變系統UI的顯示,這里傳入View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN和View.SYSTEM_UI_FLAG_LAYOUT_STABLE就表示活動的布局會顯示在狀態欄上面,最后調用一下setStatusBarColor()方法將狀態欄設置成透明色。

僅僅這些代碼就可以實現讓背景圖和狀態欄融合到一起的效果了。

不過,如果運行一下程序,你會發現還是有些問題,界面的頭布局幾乎和系統狀態欄緊貼到一起了,這是由于系統狀態欄已經成為我們布局的一部分,因此沒有單獨為它留空間。當然,這個問題也是非常好解決的,借助android:fitsSystemWindows屬性就可以了。

見代碼:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/colorPrimary">
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:fitsSystemWindows="true">
    </LinearLayout>
</FrameLayout>

這樣就可以了。

以上這篇Android 實現背景圖和狀態欄融合方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女