Android沉浸状态栏的简单应用以及包含fragment沉浸冲突的解决方法
来源:互联网 发布:起重机制造 知乎 编辑:程序博客网 时间:2024/06/02 05:29
前言:Google从android kitkat(Android 4.4)开始,给我们开发者提供了一套能透明的系统ui样式给状态栏和导航栏,这样的话就不用向以前那样每天面对着黑乎乎的上下两条黑栏了,还可以调成跟Activity一样的样式,形成一个完整的主题,和IOS7.0以上系统一样了。
1.首先看看沉浸式状态栏的效果
步骤1
在BaseActivity的里面加入判断系统版本,方便其他的Activity使用
public class BaseActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initState(); setContentView(R.layout.activity_base); } private void initState() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = getWindow(); window.setFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } }}
步骤2 MainActivity的xml文件,我在里面写了一个通用的title,请注意title里面的两个属性
android:fitsSystemWindows=”true”
android:clipToPadding=”true”
<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" tools:context="com.example.hyyx.testdemo.MainActivity"> <include layout="@layout/title_bar" /> <Button android:id="@+id/btn_main" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="跳转"/></LinearLayout>
title的xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent" android:fitsSystemWindows="true" android:clipToPadding="true" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="45dp" > <TextView android:id="@+id/title_bar_tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="标题" android:textColor="@color/colorPrimaryDark" android:textSize="18sp" /> </RelativeLayout></LinearLayout>
对了,还需要在AndroidManifest对app的theme进行一个更换
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
经过上面的操作,你的app就可以完成简单沉浸式的效果了。
2.很多人在activty中嵌套fragment时,进行沉浸式的时候标题栏可能会被顶上去,造成沉浸式效果很难看。咱们先看个效果图,是不是遇到过?
这里我有一个简单的解决方案,仅供大家参考,就是在放fragment的布局上面,加一个view,不废话看代码。
先看xml的代码
<?xml version="1.0" encoding="utf-8"?><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" tools:context="com.example.hyyx.testdemo.SecActivity"> <View android:id="@+id/statu_bar" android:layout_width="match_parent" android:layout_height="20dp" android:background="@color/colorPrimary" /> <FrameLayout android:id="@+id/frame" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"></FrameLayout> <Button android:id="@+id/btn_one" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Fragment-1" /> <Button android:id="@+id/btn_two" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Fragment-2" /></LinearLayout>
activit里面的代码
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sec); statusBar = findViewById(R.id.statu_bar); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { statusBar.setVisibility(View.VISIBLE); setTranslucentStatus(true); //还有设置View的高度,因为每个型号的手机状态栏高度都不相同 } else { statusBar.setVisibility(View.GONE); } fragmentManager = this.getSupportFragmentManager(); transaction = fragmentManager.beginTransaction(); transaction.add(R.id.frame, new MyFragment()); transaction.commit(); ((Button) findViewById(R.id.btn_one)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { statusBar.setBackgroundColor(getResources().getColor(R.color.colorPrimary)); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.frame, new MyFragment()); transaction.commit(); } } ); ((Button) findViewById(R.id.btn_two)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { statusBar.setBackgroundColor(getResources().getColor(R.color.colorAccent)); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.frame, new MyFragmentTwo()); transaction.commit(); } } ); } @TargetApi(19) private void setTranslucentStatus(boolean on) { Window win = getWindow(); WindowManager.LayoutParams winParams = win.getAttributes(); final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; if (on) { winParams.flags |= bits; } else { winParams.flags &= ~bits; } win.setAttributes(winParams); }}
这个方法比较投机耍滑,判断SDK版本是否大于等于19,大于就让他显示,小于就要隐藏,然后根据fragment的标题颜色,改变view的背景,实现沉浸式状态栏的效果。我也看过很多资料,很少有人分享解决这个问题的方法,所以各位看官先将就的看看,毕竟也是个解决办法。如果有什么问题,欢迎大家指点和一起谈论,小弟拜谢!
- Android沉浸状态栏的简单应用以及包含fragment沉浸冲突的解决方法
- Android沉浸式以及虚拟键冲突的解决方法
- Android沉浸式状态栏的简单实现
- Android沉浸式状态栏的简单实现
- Android 沉浸式状态栏的简单实现
- Android状态栏沉浸的实现
- Android状态栏的沉浸设置
- ToolBar、沉浸式状态栏的应用(简单粗暴)
- 关于Android沉浸式状态栏的解决方法之一(伪沉浸式)
- Android 沉浸式状态栏实现,以及遇到的问题
- 关于Android沉浸式状态栏的设置以及相关问题
- Android沉浸式通知栏的一个开源库SystemBarTint简单使用,android沉浸式状态栏
- 沉浸式状态栏--让你的Android应用更加漂亮
- android沉浸式状态栏的最简单实现
- android沉浸式状态栏的最简单实现
- 沉浸状态栏的实现
- 沉浸式状态栏效果的简单实现
- 沉浸式状态栏简单的方法
- 序列化和反序列化(model和dictionary互转)
- PHP安全编程之留心后门URL
- compileSdkVersion, minSdkVersion 和 targetSdkVersion,坑爹的Android studio
- Session 和 Cookie
- 数据结构实验之数组二:稀疏矩阵
- Android沉浸状态栏的简单应用以及包含fragment沉浸冲突的解决方法
- td 的 colspan 和 rowspan
- Ubuntu 下Caffe的安装过程 -- 之二
- Maven+Spring+Spring MVC+MyBatis+MySQL,搭建SSM框架环境
- hdu5816 Hearthstone 状压dp
- 竖式问题
- POJ1106(叉积应用)
- 大数据IMF传奇行动绝密课程第27课:Spark on Yarn彻底解密
- windows 当你pip安装script等模块很坑之后