仿IOS沉浸式状态栏实现
程序员文章站
2022-04-12 20:17:57
很简单,在baseactivity添加代码:
@override
protected void oncreate(bundle savedinstancestate) {...
很简单,在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="https://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"> </textview></imageview></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"> </textview></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"> </textview></imageview></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方式,简单易上手。