Android沉浸状态栏的实现,支持4.4以上

方法一:分享一个实现沉浸的工具类,用起来很简单

public class ImmersedStatusbarUtils {
    /**
     * 在{@link Activity#setContentView}之后调用
     *
     * @param activity
     *            要实现的沉浸式状态栏的Activity
     * @param titleViewGroup
     *            头部控件的ViewGroup,若为null,整个界面将和状态栏重叠
     */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    public static void initAfterSetContentView(Activity activity,
                                               View titleViewGroup) {
        if (activity == null)
            return;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = activity.getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        if (titleViewGroup == null)
            return;
        // 设置头部控件ViewGroup的PaddingTop,防止界面与状态栏重叠
        int statusBarHeight = getStatusBarHeight(activity);
        titleViewGroup.setPadding(0, statusBarHeight, 0, 0);

    }

    /**
     * 获取状态栏高度
     *
     * @param context
     * @return
     */
    private static int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier(
                "status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}
在activity的oncreate()方法的setContentView之后进行设置
setContentView(R.layout.activity_news_info);
ImmersedStatusbarUtils.initAfterSetContentView(this, rlTit);

对应的布局文件

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

 

    <LinearLayout

        android:id="@+id/lin"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:background="#ff123654" >

 

        <TextView

            android:id="@+id/title"

            android:layout_width="match_parent"

            android:layout_height="45.0dp"

            android:background="#ff123654"

            android:gravity="center"

            android:text="我是头部控件"

            android:textColor="#ffffffff" />

    </LinearLayout>

 

</LinearLayout>

之前沉浸网上有很多种,但是如果顶部是Edittext这种网上很多都是不可以解决的,上面这种方式是可以解决顶部为Edittext的问题的

方式二:借鉴郭霖大神的文章:http://blog.csdn.net/guolin_blog/article/details/51763825点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值