亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android 實(shí)現(xiàn)背景圖和狀態(tài)欄融合方法

 更新時(shí)間:2018年01月24日 11:05:30   作者:童童童童思宇  
下面小編就為大家分享一篇Android 實(shí)現(xiàn)背景圖和狀態(tài)欄融合方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

我們先看一下代碼:

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);
      ...
  }

由于這個(gè)功能是Android5.0及以上的系統(tǒng)才支持的,因此我們先在代碼中做一個(gè)系統(tǒng)版本號(hào)的判斷,只有當(dāng)版本號(hào)大于或等于21的時(shí)候,也就是5.0及以上系統(tǒng)時(shí)才會(huì)執(zhí)行后面的代碼。

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

僅僅這些代碼就可以實(shí)現(xiàn)讓背景圖和狀態(tài)欄融合到一起的效果了。

不過,如果運(yùn)行一下程序,你會(huì)發(fā)現(xiàn)還是有些問題,界面的頭布局幾乎和系統(tǒng)狀態(tài)欄緊貼到一起了,這是由于系統(tǒng)狀態(tài)欄已經(jīng)成為我們布局的一部分,因此沒有單獨(dú)為它留空間。當(dāng)然,這個(gè)問題也是非常好解決的,借助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 實(shí)現(xiàn)背景圖和狀態(tài)欄融合方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論