android多个通知栏,Android开发中怎么实现一个沉浸式通知栏

Android开发中怎么实现一个沉浸式通知栏

发布时间:2020-12-01 16:49:00

来源:亿速云

阅读:123

作者:Leah

Android开发中怎么实现一个沉浸式通知栏?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

①DrawerLayout+Toolbar

添加依赖库(谷歌提供)compile 'com.android.support:design:25.3.1'

布局代码1:使用 DrawerLayout做最外层,引入NavigationView侧边抽屉控件<?xml  version="1.0" encoding="utf-8"?>

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

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

android:id="@+id/id_drawerlayout"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.tnnowu.android.demo17032801.MainActivity">

android:id="@+id/id_navigationview"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_gravity="left"

app:itemTextColor="@color/c_light_gray3" />

布局代码2:里层嵌套Toolbar<?xml  version="1.0" encoding="utf-8"?>

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"

android:layout_height="wrap_content"

android:background="#30469b"

android:paddingTop="@dimen/toolbar_padding_top"

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

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

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:text="ToolBar版"

android:textSize="20sp" />

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/goToActionBar"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:text="切换到ActionBar版" />

Style样式:无ActionBar

@color/colorPrimary

@color/colorPrimaryDark

@color/colorAccent

主程序代码:除了要在onCreate()里面初始化 DrawerLayout、NavigationView、Toolbar控件 即initViews(),还要在onCreate()里面添加手机系统版本判断和相应的样式适配initImmersive()private void initViews() {

mDrawerLayout = (DrawerLayout) findViewById(R.id.id_drawerlayout);

mNagigationView = (NavigationView) findViewById(R.id.id_navigationview);

mNagigationView.inflateHeaderView(R.layout.header_nav);

mNagigationView.inflateMenu(R.menu.menu_nav);

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

mBtn = (Button) findViewById(R.id.goToActionBar);

mToolbar.setTitle("");

if (mToolbar != null) {

setSupportActionBar(mToolbar);

}

ActionBarDrawerToggle mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.open, R.string.close);

mActionBarDrawerToggle.syncState();

mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);

mBtn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

startActivity(new Intent(MainActivity.this, DemoActionBarActivity.class));

}

});

}

private void initImmersive() {

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

WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();

localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);

if (Build.VERSION.SDK_INT 

//将侧边栏顶部延伸至status bar

mDrawerLayout.setFitsSystemWindows(true);

//将主页面顶部延伸至status bar;虽默认为false,但经测试,DrawerLayout需显示设置

mDrawerLayout.setClipToPadding(false);

}

}

}

这样Drawlayout + Toolbar就实现了样式改变。

②ActionBar

布局代码<?xml  version="1.0" encoding="utf-8"?>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@color/colorPrimary"

android:fitsSystemWindows="true"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@color/c_light_white">

android:id="@+id/goBack"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:text="返回到ToolBar版" />

Style样式:有ActionBar

@color/colorPrimary

@color/colorPrimaryDark

@color/colorAccent

主程序代码:public class DemoActionBarActivity extends AppCompatActivity {

private Button mBtn;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main_actionbar);

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

WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();

localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);

}

initView();

}

private void initView() {

mBtn = (Button) findViewById(R.id.goBack);

mBtn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

finish();

}

});

}

}

看完上述内容,你们掌握Android开发中怎么实现一个沉浸式通知栏的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值