android沉浸状态栏工具,Android之ToolBar和自定义ToolBar实现沉浸式状态栏

本文介绍了在Android KitKat及以上版本中如何实现沉浸式状态栏,通过两种不同的方式结合ToolBar来达到与APP主题融合的效果。第一种方式是直接设置ToolBar,第二种方式是自定义布局并设置状态栏透明。这两种方法都需要API 19以上才能运行。
摘要由CSDN通过智能技术生成

沉浸式状态栏确切的说应该叫做透明状态栏。一般情况下,状态栏的底色都为黑色,而沉浸式状态栏则是把状态栏设置为透明或者半透明。

沉浸式状态栏是从android Kitkat(Android 4.4)开始出现的,它可以被设置成与APP顶部相同的颜色,这就使得切换APP时,整个界面就好似切换到了与APP相同的风格样式一样。在内容展示上会显得更加美观。

本博客主要说的是结合ToolBar来实现状态栏的两种实现方式,效果如图:

2cef6a95e5fbfc97ee61bd364451acd9.gif

前提条件是 Api得大于等于19(4.4版本以上)

方式1:

布局 toolbar1.xml

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/toolbar"

android:layout_width="match_parent"

app:title="App Title"

app:subtitle="Sub Title"

app:navigationIcon="@android:drawable/ic_input_add"

android:layout_height="wrap_content"

android:background="?attr/colorPrimary"

android:fitsSystemWindows="true"

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:text="沉浸式状态栏"

android:textSize="30sp" />

Activity.Java

public class TooBarStatusActivity1 extends AppCompatActivity{

@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);//去标题

setContentView(R.layout.toolbar_layout);

//透明状态栏

getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

//透明导航栏

getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

//透明导航栏

Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);

mToolbar.setTitle("App Title"); //设置Toolbar标题

mToolbar.setSubtitle("Sub Title"); //设置Toolbar 副标题

mToolbar.setLogo(R.mipmap.ic_launcher);//设置Toolbar的Logo

mToolbar.setNavigationIcon(R.mipmap.abc_ic_ab_back_mtrl_am_alpha);

setSupportActionBar(mToolbar);

}

}

方式2:

布局 toolbar2.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@android:color/white"

android:orientation="vertical"

>

android:layout_width="match_parent"

android:layout_height="100dp"

android:background="@color/colorAccent"

android:fitsSystemWindows="true"

android:clipToPadding="true"

android:text="自定义的ToolBar布局"

android:textSize="20sp"

android:gravity="center_vertical"

/>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#FFFF00"

android:text="主布局"

android:textSize="22sp"

android:gravity="center"

/>

Activity.Java

public class TooBarStatusActivity2 extends AppCompatActivity {

@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.toolbar_layout2);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

//透明状态栏

getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

//透明导航栏

getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

}

}

}

style.xml

@color/colorPrimary

@color/colorPrimaryDark

@color/colorAccent

false

true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值