四、【沉浸式状态栏】

一、Andriod 4.4 及以下的版本开始支持 1.设置方式Activity设置

  //去掉标题
  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.activity_two);
复制代码
  //当系统版本为4.4或者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);
  }
复制代码
  1. Activity布局文件设置
android:fitsSystemWindows="true"
android:clipToPadding="true"
复制代码

例:activity 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    tools:context=".MainActivity">
 <RelativeLayout
  android:fitsSystemWindows="true"
  android:clipToPadding="true"
  android:layout_width="match_parent"
复制代码

二、动态实现,xml文件中不需要额外设置

public class TwoActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //去掉标题
  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.activity_two);
  //当系统版本为4.4或者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);
   LinearLayout linear_bar=(LinearLayout)findViewById(R.id.linear_bar);
   linear_bar.setVisibility(View.VISIBLE);
   int statusHeight=getStatusBarHeight();
   LinearLayout.LayoutParams params=(LinearLayout.LayoutParams )linear_bar.getLayoutParams();
   params.height=statusHeight;
   linear_bar.setLayoutParams(params);
  }
 }
 /**
  * 获取状态栏的高度
  * @return
  */
 private int getStatusBarHeight(){
  try
  {
   Class<?> c=Class.forName("com.android.internal.R$dimen");
   Object obj=c.newInstance();
   Field field=c.getField("status_bar_height");
   int x=Integer.parseInt(field.get(obj).toString());
   return getResources().getDimensionPixelSize(x);
  }catch(Exception e){
   e.printStackTrace();
  }
  return 0;
 }
}
复制代码

三、依赖第三方类库(SystemBarTint) 地址:github.com/jgilfelt/Sy… 关联库:compile'com.readystatesoftware.systembartint:systembartint:1.0.3'

//当系统版本为4.4或者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);
   SystemBarTintManager tintManager = new SystemBarTintManager(this);
   // 激活状态栏
   tintManager.setStatusBarTintEnabled(true);
   // enable navigation bar tint 激活导航栏
   tintManager.setNavigationBarTintEnabled(true);
   //设置系统栏设置颜色
   //tintManager.setTintColor(R.color.red);
   //给状态栏设置颜色
   tintManager.setStatusBarTintResource(R.color.middle_color);
   // 设置导航栏设置资源
   tintManager.setNavigationBarTintResource(R.color.androidColorE);
  }
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值