当前位置: 移动技术网 > 移动技术>移动开发>Android > Android4.4+ 实现半透明状态栏(Translucent Bars)

Android4.4+ 实现半透明状态栏(Translucent Bars)

2019年07月24日  | 移动技术网移动技术  | 我要评论

android从4.4(kitkat) 开始进行了一些视觉上的改善和提升,其中包括让状态栏(status bar)和下方导航栏(navigation bar)进行半透明处理,可以使app内容向上下延伸,使整个画面的利用度大幅度提升,本篇就来说说这个“半透明状态栏”(translucent bars)。

简单做了个demo效果如下图

*这里解释个误区,国内开发者和设计师经常把这种半透明效果称为沉浸式状态栏这是不对的, 沉浸式immersive mode,官方解释为hiding all system ui根本不是这种半透明的效果。

下面说说如何使用这种效果:

1、在oncreate里面代码设置半透明的属性,由于只有android 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);
     }

2、在这个界面上我去掉了actionbar,实现方式有很多,这里我使用的是在style里去掉。

<style name="apptheme" parent="theme.appcompat.light.noactionbar">
    <!-- customize your theme here. -->
  </style>

3、这个部分需要留意一下,如果希望app的显示内容正常和滚动透明化需要加上android:fitssystemwindows=”true”和android:cliptopadding=”false”的属性,建议你把这两个属性好好试试加上与否的区别。

<scrollview 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:fitssystemwindows="true"
  android:cliptopadding="false"
  android:background="#795548"
  tools:context=".defaultactivity" >

  <textview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textsize="18sp"
    android:textcolor="#ffffff"
    android:text="@string/str" />

</scrollview>

这样一个简单的半透明化效果就实现了

详细源码:

layout

<scrollview 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:fitssystemwindows="true"
  android:cliptopadding="false"
  android:background="#795548"
  tools:context=".defaultactivity" >

  <textview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textsize="18sp"
    android:textcolor="#ffffff"
    android:text="@string/str" />

</scrollview>

style

<resources>

  <!-- base application theme. -->
  <style name="apptheme" parent="theme.appcompat.light.noactionbar">
    <!-- customize your theme here. -->
  </style>

</resources>

mainactivity

public class mainactivity extends appcompatactivity {

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);

    if(build.version.sdk_int >= build.version_codes.kitkat) {
      //透明状态栏
      getwindow().addflags(windowmanager.layoutparams.flag_translucent_status);
      //透明导航栏
      getwindow().addflags(windowmanager.layoutparams.flag_translucent_navigation);
     }

    setcontentview(r.layout.activity_main);



  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网