关于如何实现Android透明状态栏的总结

xiaoxiao2021-02-28  13

好久没写博客了,哈哈,言归正传,最近遇到了透明状态栏的需求,下面总结一下 ,希望能帮助到有需求的人。

我们先看下之前正常的效果,顶部是一条明显的橙色的颜色。(这是临时找的图,跟我代码的效果类似,意思一样)

为了需求和美观,我们要做成下面的样子(把顶部透明,样式基本就这样,只是找了类似的图片,动图懒得做~~~)

下面直接说代码,首先要在theme中引用的style文件写:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowActionBar">false</item> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style> 然后我把部分布局贴出来,这里我就只贴搜索框的代码,因为它是紧跟于状态栏的布局

<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <View android:id="@+id/view_topview" android:layout_width="match_parent" android:layout_height="0dp" android:padding="50dp" /> <LinearLayout android:id="@+id/ll_search" android:layout_width="match_parent" android:layout_height="50dp" android:background="#00FCAF24" android:orientation="horizontal"> <ImageView android:id="@+id/tv_city" android:layout_width="30dp" android:layout_height="30dp" android:layout_gravity="center" android:layout_marginLeft="10dp" android:background="@drawable/mulu" /> <EditText android:id="@+id/tv_search" android:layout_width="fill_parent" android:layout_height="35dp" android:layout_gravity="center" android:layout_marginBottom="2dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="3dp" android:layout_weight="1" android:background="@drawable/home_search_bg" android:cursorVisible="false" android:drawableLeft="@mipmap/search1a" android:hint="搜索商品/供应商/气源地" android:maxLength="10" android:paddingLeft="10dp" android:textColor="#333333" android:textColorHint="#929292" android:textSize="14sp" /> <ImageView android:id="@+id/iv_sao" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginRight="10dp" android:src="@drawable/xiaoxi" /> </LinearLayout> </LinearLayout> 有人会问到这个View是干嘛的,其实到现在我也没弄清楚是干嘛的,误打误撞而已,我理解的意思是(最后在总结),所以这里我们先看Fragment的代码, mRootView = inflater.inflate(R.layout.home_fragment, container, false); mTopView = mRootView.findViewById(R.id.view_topview); //设置状态栏的初始颜色 SystemStatesBarUtils.setTopViewHeightColor(getActivity(), mTopView, R.color.transparence);然后我这里是做了一个滑动布局的时候,状态栏的颜色跟随我滑动的动作颜色渐变

@Override public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) { if (y <= 0) { //设置标题的背景颜色 滑动到顶部 layout.setBackgroundColor(Color.argb(0, 252, 175, 36)); mTopView.setBackgroundColor(Color.argb(0, 252, 175, 36)); //toolbar_common.setAlpha(0); } else if (y > 0 && y <= height) { //滑动距离小于banner图的高度时,设置背景和字体颜色颜色透明度渐变 中间渐变 float scale = (float) y / height; float alpha = (205 * scale); layout.setBackgroundColor(Color.argb((int) alpha, 72, 137, 219)); mTopView.setBackgroundColor(Color.argb((int) alpha, 72, 137, 219)); //toolbar_common.setAlpha(scale); Log.e("111-------------------", "scale------" + scale + "alpha-----" + alpha); } else { //滑动到banner下面设置普通颜色 滑动到底部了 mTopView.setBackgroundColor(Color.argb(205, 72, 137, 219)); layout.setBackgroundColor(Color.argb(205, 72, 137, 219)); //toolbar_common.setAlpha(1); //toolbar_common.setBackgroundColor(ContextCompat.getColor(getActivity(),R.color.colorPrimary)); } } 这个方法的layout  就是搜索框的布局id,这是Scoollview的滑动事件设置控件的颜色跟随滑动事件而渐变

然后在Activity中别忘了添加版本的判断

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明状态栏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明导航栏 如果只是想要更改状态栏 这句可以注销 //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); }然后就完事了,对于这个Top_View,我的理解是,之前透明了ActionBar,我们在搜索框和状态栏之间加个Veiw,并且属性padding为50,那我们设置Top-View的颜色,就会填充到状态栏,然后就可以了,我是这么理解,有大神知道怎么回事的可以发表评论,毕竟我不是大神。哈哈

转载请注明原文地址: https://www.6miu.com/read-850166.html

最新回复(0)