现在的位置: 首页 > 综合 > 正文

仿IOS透明通知栏(仅支持4.4以上版本)

2018年04月28日 ⁄ 综合 ⁄ 共 1047字 ⁄ 字号 评论关闭

从4.4开始,Google为Android增加了透明状态栏和导航栏的功能,只需要加入少量代码就可以实现。如图:


在Activity的布局文件父View中添加属性:android:fitsSystemWindows="true"android:clipToPadding="true"即可。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:clipToPadding="true"
    android:background="#ffe5ff3e">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="16dp"
            android:textSize="18sp"
            android:text="@string/mytext" />

    </LinearLayout>
</ScrollView>

如果不想要ActionBar,可以在AndroidManifest.xml或者Activity中去除,就得到上图效果。但是,透明通知栏只支持4.4以上的系统,在4.4以下还是会显示默认的通知栏。


如果希望修改通知栏颜色,可以参考Github大牛的Demo:

https://github.com/jgilfelt/SystemBarTint

其中的library只有一个SystemBarTintManager类,可以拷贝到自己的工程中使用,这样就不用依赖那个library了。

还可以参考这篇博文:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1117/1992.html

抱歉!评论已关闭.