Android開發(fā)Activity彈框形式并且狀態(tài)欄透明

首先這需求不怎么常見,一般用于單純的彈框做需求太復(fù)雜。就好像業(yè)務(wù)是Activity的,但是UI是彈框的。
下面我們一步步來:

一、先將Activity弄成彈框的樣式

 <style name="dialog_activity" parent="Theme.AppCompat.Dialog">
    <item name="windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowLayoutInDisplayCutoutMode" tools:targetApi="o_mr1">shortEdges</item>
</style>

注意:parent="Theme.AppCompat.Dialog",它背景才有彈框的樣式

"android:windowLayoutInDisplayCutoutMode":因?yàn)槲业男枨笫菭顟B(tài)欄透明,即內(nèi)容頂?shù)綘顟B(tài)欄下面,這個(gè)是適配Android11以上的手機(jī)

二、狀態(tài)欄透明(內(nèi)容頂?shù)綘顟B(tài)下面)

在onCreate里面寫

public static void setTranslucentStatus(Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // 5.x開始需要把顏色設(shè)置透明,否則導(dǎo)航欄會呈現(xiàn)系統(tǒng)默認(rèn)的淺灰色
        Window window = activity.getWindow();
        View decorView = window.getDecorView();
        // 兩個(gè) flag 要結(jié)合使用,表示讓應(yīng)用的主體內(nèi)容占用系統(tǒng)狀態(tài)欄的空間
        int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        decorView.setSystemUiVisibility(option);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.TRANSPARENT);
        // 導(dǎo)航欄顏色也可以正常設(shè)置
        // window.setNavigationBarColor(Color.TRANSPARENT);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window window = activity.getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        attributes.flags |= flagTranslucentStatus;
        // int flagTranslucentNavigation =
        // WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
        // attributes.flags |= flagTranslucentNavigation;
        window.setAttributes(attributes);
    }
}

三、內(nèi)容占滿屏幕的設(shè)置

設(shè)置為上面,你可能會發(fā)現(xiàn)內(nèi)容沒占滿屏幕,就算你布局寫了占滿屏幕,它也是擠在中間,沒占滿寬。
需在onCreate里面做如下配置:

window.run {
        setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        setBackgroundDrawable(getDrawables(R.color.transparent))
    }

如果你還遇到其他問題,歡迎私信或者評論交流。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容