温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-12-01 16:49:00 来源:亿速云 阅读:241 作者:Leah 栏目:移动开发

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

①DrawerLayout+Toolbar

添加依赖库(谷歌提供)

compile 'com.android.support:design:25.3.1'

布局代码1:使用 DrawerLayout做最外层,引入NavigationView侧边抽屉控件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
 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">

 <include layout="@layout/content_layout" />

 <android.support.design.widget.NavigationView
 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" />

</android.support.v4.widget.DrawerLayout>

布局代码2:里层嵌套Toolbar

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">

 <android.support.v7.widget.Toolbar
 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">

 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:text="ToolBar版"
  android:textSize="20sp" />

 </android.support.v7.widget.Toolbar>

 <!--内容显示布局-->
 <RelativeLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <Button
  android:id="@+id/goToActionBar"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true"
  android:text="切换到ActionBar版" />
 </RelativeLayout>

</LinearLayout>

Style样式:无ActionBar

<style name="AppThemeNoActionBar" 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>
</style>

主程序代码:除了要在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 < Build.VERSION_CODES.LOLLIPOP) {
  //将侧边栏顶部延伸至status bar
  mDrawerLayout.setFitsSystemWindows(true);
  //将主页面顶部延伸至status bar;虽默认为false,但经测试,DrawerLayout需显示设置
  mDrawerLayout.setClipToPadding(false);
  }
 }
 }

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

 ②ActionBar

布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@color/colorPrimary"
 android:fitsSystemWindows="true"
 android:orientation="vertical">

 <!--内容显示布局-->
 <RelativeLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@color/c_light_white">

 <Button
  android:id="@+id/goBack"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true"
  android:text="返回到ToolBar版" />
 </RelativeLayout>

</LinearLayout>

Style样式:有ActionBar

<style name="AppThemeActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
 <!-- Customize your theme here. -->
 <item name="colorPrimary">@color/colorPrimary</item>
 <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
 <item name="colorAccent">@color/colorAccent</item>
</style>

主程序代码:

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开发中怎么实现一个沉浸式通知栏的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI