Android沉浸式状态栏实现

摘要:
xmlversion=“1.0”encoding=“utf-8”?

在安卓开发当中,顶部的状态栏很多时候是和我们自己所设定的安卓背景颜色不相同的,看起来就十分别扭,就如同下图所示,状态栏是深绿色,我们的背景却是一个十分好看的渐变颜色:
Android沉浸式状态栏实现第1张

在使用沉浸式状态栏之后的界面如下:

Android沉浸式状态栏实现第2张

如何将顶部的状态栏设置成透明的呢,我们可以在主活动的

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>

沉浸式布局就这样实现啦,相对来说这个实现过程实在是太简单了!

免责声明:文章转载自《Android沉浸式状态栏实现》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇MappedByteBuffergunicorn启动fastapi命令下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

CSS 基本样式

4.1 CSS 基本概念 CSS:Cascade Style Sheet 层叠样式表。 样式表: 样式的集合,定义HTML元素显示的方式。 4.2 CSS 的使用 使用 CSS 的三种方式: 外部样式表:需要配合外部的 CSS 样式文件使用 index.html 页面 <!-- index.html --> <!DOCTYPE html...

滚动条样式修改

::-webkit-scrollbar {width:5px;//对垂直流动条有效 height:10px;//对水平流动条有效 } //定义滚动条的轨道颜色、内阴影及圆角 ::-webkit-scrollbar-track{-webkit-box-shadow:inset 0 0 6px rgba(0,0,0,.3);background-color:t...

.NET跨平台机制一(mono for android配置教程)

    忙完了毕业设计,坐等毕业,终于有时间可以玩玩.NET的跨平台机制了,当然了.NET跨平台主要就是Linux,所以就蛮有心思的去配置了下mono for android的开发环境。       首先,准备工作要做足,运行时,虚拟机,模拟器都要先下载好了。 前期准备,先看看安卓模拟器的配置(已经会配置的略过..)配置教程网络上也很多,我这里就稍微盖过...

移动端的几个面试小问题

1. 安卓下大面积触摸会导致触发touchmove的问题   判断一下touchstart的上一次位置和当前位置是否一样,一样就使move return掉 <body> <div class="page"> <div id="box"></div> </div> <script ty...

android学习ProgressBar的简单使用

android 提供的ProgressBar控件分为两种,一种是不带进度的进度条,一种是带进度的进度条,如果可以计算任务的完成量那么就用带进度条的,如果无法计算任务量,那么就使用不带进度的进度条。ProgressBar如果说只使用系统提供的,那就很简单,就只有那些属性方法,但是感觉比较难得就是ProgressBar的样式,一般做应用都不会直接使用系统提供的...

WPF自定义窗口(Windows Server 2012 Style)

先上图 新建CustomControl,名:HeaderedWindow ThemesGeneric.aml文件夹下加入 笔刷,转换器 1 <SolidColorBrush x:Key="ActiveBackground" Color="#FF66CBEA"/> 2 <SolidColorBrush x:Key="De...