Android沉浸式狀態欄實現

来源:https://www.cnblogs.com/geeksongs/archive/2019/12/12/12031785.html
-Advertisement-
Play Games

在安卓開發當中,頂部的狀態欄很多時候是和我們自己所設定的安卓背景顏色不相同的,看起來就十分彆扭,就如同下圖所示,狀態欄是深綠色,我們的背景卻是一個十分好看的漸變顏色: 在使用沉浸式狀態欄之後的界面如下: 如何將頂部的狀態欄設置成透明的呢,我們可以在主活動的 onCreate() 方法當中輸入以下代碼 ...


在安卓開發當中,頂部的狀態欄很多時候是和我們自己所設定的安卓背景顏色不相同的,看起來就十分彆扭,就如同下圖所示,狀態欄是深綠色,我們的背景卻是一個十分好看的漸變顏色:

 

在使用沉浸式狀態欄之後的界面如下:

 

 

 

如何將頂部的狀態欄設置成透明的呢,我們可以在主活動的

onCreate()

方法當中輸入以下代碼:

if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT)
        {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        }

同時在xml文件中所對應的漸變背景控制項當中添加以下屬性:

    android:fitsSystemWindows="true"
    android:clipToPadding="true"

本身我這個xml界面的代碼如下所示,我們只需要看前面最上方的那個有關漸變背景的相對佈局代碼,後面的代碼有興趣的同學也可以看看我這個佈局是如何實現的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragment4">

<RelativeLayout
  //就是這裡這個相對佈局這裡,我引入了這個漸變的背景色,因此在這裡我們引入這兩個屬性就好啦
    android:background="@drawable/back"
    android:layout_width="match_parent"
    android:layout_height="200dp">
    <de.hdodenhof.circleimageview.CircleImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"

        android:layout_marginLeft="10dp"
        android:layout_alignParentBottom="true"
        android:id="@+id/profile_image"
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:src="@drawable/wo"
        app:civ_border_width="2dp"
        app:civ_border_color="#FF000000"/>

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="13dp"
        android:layout_marginBottom="48dp"
        android:layout_toRightOf="@+id/profile_image"
        android:text="用戶:直男Geek"
        android:textColor="@color/colorAccent"
        android:textSize="25dp" />

    <TextView
        android:textColor="@color/colorAccent"
        android:layout_marginBottom="25dp"
        android:layout_marginLeft="120dp"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="個性簽名:沒有直男,這個世界將會充滿愛" />
</RelativeLayout>
    <LinearLayout
        android:background="#ffffff"
        android:id="@+id/gj_recruit3"
        android:layout_width="match_parent"
        android:layout_height="50dip"

        android:focusableInTouchMode="true"
        android:clickable="true"
        android:orientation="horizontal"
        android:padding="7dip">
        <ImageView
            android:layout_gravity="center"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/wode1"/>
        <TextView
            android:textSize="20dp"
            android:textColor="@color/colorPrimaryDark"
            android:layout_marginLeft="10dp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我發佈的口紅"/>




    </LinearLayout>
    <LinearLayout
        android:background="#ffffff"

        android:layout_width="match_parent"
        android:layout_height="50dip"

        android:focusableInTouchMode="true"
        android:clickable="true"
        android:orientation="horizontal"
        android:padding="7dip">
        <ImageView
            android:layout_gravity="center"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/wode2"/>
        <TextView
            android:textSize="20dp"
            android:textColor="@color/colorPrimaryDark"
            android:layout_marginLeft="10dp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我收藏的口紅"/>




    </LinearLayout>
    <LinearLayout
        android:background="#ffffff"

        android:layout_width="match_parent"
        android:layout_height="50dip"

        android:focusableInTouchMode="true"
        android:clickable="true"
        android:orientation="horizontal"
        android:padding="7dip">
        <ImageView
            android:layout_gravity="center"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/liulan"/>
        <TextView
            android:textSize="20dp"
            android:textColor="@color/colorPrimaryDark"
            android:layout_marginLeft="10dp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="瀏覽記錄"/>




    </LinearLayout>
    <LinearLayout
        android:background="#ffffff"

        android:layout_width="match_parent"
        android:layout_height="50dip"

        android:focusableInTouchMode="true"
        android:clickable="true"
        android:orientation="horizontal"
        android:padding="7dip">
        <ImageView
            android:layout_gravity="center"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/huifu"/>
        <TextView
            android:textSize="20dp"
            android:textColor="@color/colorPrimaryDark"
            android:layout_marginLeft="10dp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="最新回覆"/>




    </LinearLayout>

</LinearLayout>

更改後的代碼為:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragment4">

<RelativeLayout
    android:fitsSystemWindows="true"
    android:clipToPadding="true"
    android:background="@drawable/back"
    android:layout_width="match_parent"
    android:layout_height="200dp">
    <de.hdodenhof.circleimageview.CircleImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"

        android:layout_marginLeft="10dp"
        android:layout_alignParentBottom="true"
        android:id="@+id/profile_image"
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:src="@drawable/wo"
        app:civ_border_width="2dp"
        app:civ_border_color="#FF000000"/>

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="13dp"
        android:layout_marginBottom="48dp"
        android:layout_toRightOf="@+id/profile_image"
        android:text="用戶:直男Geek"
        android:textColor="@color/colorAccent"
        android:textSize="25dp" />

    <TextView
        android:textColor="@color/colorAccent"
        android:layout_marginBottom="25dp"
        android:layout_marginLeft="120dp"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="個性簽名:沒有直男,這個世界將會充滿愛" />
</RelativeLayout>
    <LinearLayout
        android:background="#ffffff"
        android:id="@+id/gj_recruit3"
        android:layout_width="match_parent"
        android:layout_height="50dip"

        android:focusableInTouchMode="true"
        android:clickable="true"
        android:orientation="horizontal"
        android:padding="7dip">
        <ImageView
            android:layout_gravity="center"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/wode1"/>
        <TextView
            android:textSize="20dp"
            android:textColor="@color/colorPrimaryDark"
            android:layout_marginLeft="10dp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我發佈的口紅"/>




    </LinearLayout>
    <LinearLayout
        android:background="#ffffff"

        android:layout_width="match_parent"
        android:layout_height="50dip"

        android:focusableInTouchMode="true"
        android:clickable="true"
        android:orientation="horizontal"
        android:padding="7dip">
        <ImageView
            android:layout_gravity="center"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/wode2"/>
        <TextView
            android:textSize="20dp"
            android:textColor="@color/colorPrimaryDark"
            android:layout_marginLeft="10dp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我收藏的口紅"/>




    </LinearLayout>
    <LinearLayout
        android:background="#ffffff"

        android:layout_width="match_parent"
        android:layout_height="50dip"

        android:focusableInTouchMode="true"
        android:clickable="true"
        android:orientation="horizontal"
        android:padding="7dip">
        <ImageView
            android:layout_gravity="center"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/liulan"/>
        <TextView
            android:textSize="20dp"
            android:textColor="@color/colorPrimaryDark"
            android:layout_marginLeft="10dp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="瀏覽記錄"/>




    </LinearLayout>
    <LinearLayout
        android:background="#ffffff"

        android:layout_width="match_parent"
        android:layout_height="50dip"

        android:focusableInTouchMode="true"
        android:clickable="true"
        android:orientation="horizontal"
        android:padding="7dip">
        <ImageView
            android:layout_gravity="center"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/huifu"/>
        <TextView
            android:textSize="20dp"
            android:textColor="@color/colorPrimaryDark"
            android:layout_marginLeft="10dp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="最新回覆"/>




    </LinearLayout>

</LinearLayout>

沉浸式佈局就這樣實現啦,相對來說這個實現過程實在是太簡單了!


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1.vim /etc/my.cnf 2.在[mysqld]下添加一行skip-grant-tables,然後保存並退出。 3.重啟mysql服務:service mysqld restart。 4.不用密碼直接登錄 mysql -u root 5.通過SQL修改root密碼 MySQL> UPDAT ...
  • 當打開sql server2008企業管理器的時候,出現報錯“評估期已過。有關如何升級的測試版軟體的信息.....” 修改註冊表:HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Microsoft SQL Server/100/ConfigurationState里的 C ...
  • 問題描述:spool讓我想起來了spooling假離線,但是這個spool是oracle下的命令,將select查詢出來的數據列印出來 1.linuxi下 spool +路徑+文件名,這裡的文件如果不存在,是可以自動生成的 spool /home/oracle/b.txt select查詢語句 sp ...
  • 一、redis簡介 一般學習,最好先去官網,之所以建議看官網,是因為這是一手的學習資料,其他資料都最多只能算二手,一手資料意味著最權威,準確性最高。https://redis.io/topics/introduction。如果像我一樣,英語不好的童鞋,不要緊,咋們用Chrome瀏覽器,翻譯成中文。E ...
  • 錯誤的意思是listener 不知道連接解析器中的請求服務,這裡要說靜態監聽和動態監聽了動態註冊是在instance啟動的時候PMON進程根據init.ora中的instance_name,service_names兩個參數將實例和服務動態註冊到listener中。靜態註冊就是實例啟動時讀取list ...
  • oracle資料庫實例啟動過程分三個步驟,分別是啟動實例,載入資料庫,打開數據。 1.NOMOUNT模式:這種模式只會創建實例,不會打開任何的數據文件,用戶要以sysdba的身份登錄,才具有關閉和啟動資料庫實例的許可權,根據參數文件啟動實例 2.MOUNT模式:啟動實例,裝載資料庫但是保持資料庫關閉的 ...
  • Redis中幾個“看似”高大上的概念,經常有人提到,某些好事者喜歡死扣概念,實戰沒多少,嘴巴裡冒出來的全是高大上的名詞,個人一向鄙視概念黨,呵呵,尼瑪! 其實這幾個概念:緩存穿透/緩存擊穿/緩存雪崩,有一個共通的相似之處,就是高併發下,某些原因導致緩存層失去了保護,導致後端的持久化層(資料庫)承擔較 ...
  • convert(char(10),convert(datetime, H.TOEX_FIN_REJECT_TIMR)+1,20), ...
一周排行
    -Advertisement-
    Play Games
  • 1. 說明 /* Performs operations on System.String instances that contain file or directory path information. These operations are performed in a cross-pla ...
  • 視頻地址:【WebApi+Vue3從0到1搭建《許可權管理系統》系列視頻:搭建JWT系統鑒權-嗶哩嗶哩】 https://b23.tv/R6cOcDO qq群:801913255 一、在appsettings.json中設置鑒權屬性 /*jwt鑒權*/ "JwtSetting": { "Issuer" ...
  • 引言 集成測試可在包含應用支持基礎結構(如資料庫、文件系統和網路)的級別上確保應用組件功能正常。 ASP.NET Core 通過將單元測試框架與測試 Web 主機和記憶體中測試伺服器結合使用來支持集成測試。 簡介 集成測試與單元測試相比,能夠在更廣泛的級別上評估應用的組件,確認多個組件一起工作以生成預 ...
  • 在.NET Emit編程中,我們探討了運算操作指令的重要性和應用。這些指令包括各種數學運算、位操作和比較操作,能夠在動態生成的代碼中實現對數據的處理和操作。通過這些指令,開發人員可以靈活地進行算術運算、邏輯運算和比較操作,從而實現各種複雜的演算法和邏輯......本篇之後,將進入第七部分:實戰項目 ...
  • 前言 多表頭表格是一個常見的業務需求,然而WPF中卻沒有預設實現這個功能,得益於WPF強大的控制項模板設計,我們可以通過修改控制項模板的方式自己實現它。 一、需求分析 下圖為一個典型的統計表格,統計1-12月的數據。 此時我們有一個需求,需要將月份按季度劃分,以便能夠直觀地看到季度統計數據,以下為該需求 ...
  • 如何將 ASP.NET Core MVC 項目的視圖分離到另一個項目 在當下這個年代 SPA 已是主流,人們早已忘記了 MVC 以及 Razor 的故事。但是在某些場景下 SSR 還是有意想不到效果。比如某些靜態頁面,比如追求首屏載入速度的時候。最近在項目中回歸傳統效果還是不錯。 有的時候我們希望將 ...
  • System.AggregateException: 發生一個或多個錯誤。 > Microsoft.WebTools.Shared.Exceptions.WebToolsException: 生成失敗。檢查輸出視窗瞭解更多詳細信息。 內部異常堆棧跟蹤的結尾 > (內部異常 #0) Microsoft ...
  • 引言 在上一章節我們實戰了在Asp.Net Core中的項目實戰,這一章節講解一下如何測試Asp.Net Core的中間件。 TestServer 還記得我們在集成測試中提供的TestServer嗎? TestServer 是由 Microsoft.AspNetCore.TestHost 包提供的。 ...
  • 在發現結果為真的WHEN子句時,CASE表達式的真假值判斷會終止,剩餘的WHEN子句會被忽略: CASE WHEN col_1 IN ('a', 'b') THEN '第一' WHEN col_1 IN ('a') THEN '第二' ELSE '其他' END 註意: 統一各分支返回的數據類型. ...
  • 在C#編程世界中,語法的精妙之處往往體現在那些看似微小卻極具影響力的符號與結構之中。其中,“_ =” 這一組合突然出現還真不知道什麼意思。本文將深入剖析“_ =” 的含義、工作原理及其在實際編程中的廣泛應用,揭示其作為C#語法奇兵的重要角色。 一、下劃線 _:神秘的棄元符號 下劃線 _ 在C#中並非 ...