仿IOS沉浸式状态栏实现

该博客介绍了如何在Android中实现类似iOS的沉浸式状态栏效果。通过在BaseActivity中添加代码,调整TitleBuilder.java和root_view.xml,设置适当的paddingTop属性来避免标题栏侵入状态栏区域。同时,文章提醒注意状态栏颜色的设定,并推荐使用paddingTop方法,因为这种方法简单且易于操作。附带了一个DEMO供读者下载参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很简单,在BaseActivity添加代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());

        //判断当前SDK版本号,如果是4.4以上,就是支持沉浸式状态栏的
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

        TitleBuilder titleBuilder = new TitleBuilder(this);
        titleBuilder.setTitleText(res.getString(R.string.app_name));

   }

TitleBuilder.java

public TitleBuilder(final Activity context) {
        View root = TypefaceUtil.getRootView(context);
        isLinearLayout = root instanceof LinearLayout;
        if (isLinearLayout) {
            LinearLayout rootView = (LinearLayout) root;
//          rootView.setClipToPadding(true);
//          rootView.setFitsSystemWindows(true);
//          rootView.setBackgroundColor(context.getResources().getColor(R.color.subject_bg));
            view = LayoutInflater.from(context).inflate(R.layout.top_toolbar, null);
            view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ConstantValue.TitleHeight));
            rootView.addView(view, 0);
          }

    }

getRootView方法:

/**
     * 从Activity 获取 rootView 根节点
     * @param context
     * @return 当前activity布局的根节点
     */
    public static View getRootView(Activity context)
    {
        return ((ViewGroup)context.findViewById(android.R.id.content)).getChildAt(0);
    }

top_toolbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_toolbar"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="@dimen/y30"
    android:background="@color/subject_bg"
    android:gravity="center_vertical"
    android:paddingTop="@dimen/y10">

    <LinearLayout
        android:id="@+id/rl_toolbar_left"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:paddingLeft="@dimen/x10"
        android:gravity="left|center_vertical">
        <ImageView
            android:id="@+id/iv_toolbar_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/common_back"/>
        <TextView
            android:id="@+id/tv_toolbar_left"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="#fff"
            android:textSize="18sp"
            android:visibility="gone"
            android:gravity="center_vertical"
            android:layout_marginLeft="@dimen/x10"/>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/rl_toolbar_center"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:gravity="center">
        <TextView
            android:id="@+id/tv_toolbar_center"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="18sp"
            android:ellipsize="end"
            android:singleLine="true"
            android:textColor="#fff"/>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_toolbar_right"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:paddingRight="@dimen/x10">
        <ImageView
            android:id="@+id/iv_toolbar_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:visibility="gone"
            android:src="@mipmap/common_back"/>
        <TextView
            android:id="@+id/tv_toolbar_right"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textColor="#fff"
            android:textSize="18sp"
            android:gravity="center"
            android:layout_alignParentRight="true"
            android:layout_toLeftOf="@+id/iv_toolbar_right"/>
    </RelativeLayout>
</LinearLayout>

这里根view一定要有个android:paddingTop=”@dimen/y10”属性,用来是标题栏看起来不会渗进状态栏里边。

注意到,TitleBuilder注释掉这两句话:

rootView.setClipToPadding(true);
rootView.setFitsSystemWindows(true);

大概意思就是在标题栏上方留出状态栏位置,但是光有这个的话状态栏的颜色是透明的,一定要和设置Activity的rootView背景色搭配使用。但是这样整个页面的背景色都变成主题色了,还得辛辛苦苦的将View的背景设置一番。

rootView.setBackgroundColor(context.getResources().getColor(R.color.subject_bg));

推荐使用paddingTop方式,简单易上手。

DEMO下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值