欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android五种隐藏状态栏和标题栏的方法

程序员文章站 2023-11-27 12:43:04
android五种隐藏状态栏和标题栏的方法 方法一: public class mainactivity extends activity {...

android五种隐藏状态栏和标题栏的方法

方法一:

public class mainactivity extends activity 
{ 
 
  @override 
  protected void oncreate(bundle savedinstancestate) 
  { 
    super.oncreate(savedinstancestate); 
 
    // 隐藏标题栏 
    requestwindowfeature(window.feature_no_title); 
    // 隐藏状态栏 
    getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, 
        windowmanager.layoutparams.flag_fullscreen); 
 
    setcontentview(r.layout.activity_main); 
  } 
 
} 

方法二:


<!-- 同时隐藏状态栏和标题栏  -->  
<activity 
  android:name="com.ysj.demo.mainactivity" 
  android:theme="@android:style/theme.notitlebar.fullscreen" 
  android:label="@string/app_name" > 
  <intent-filter> 
    <action android:name="android.intent.action.main" /> 
 
 
    <category android:name="android.intent.category.launcher" /> 
  </intent-filter> 
</activity> 

方法三:

<!-- application theme. --> 
<style name="apptheme" parent="appbasetheme"> 
  <!-- all customizations that are not specific to a particular api-level can go here. --> 
  <!-- 隐藏状态栏 --> 
  <item name="android:windowfullscreen">true</item> 
  <!-- 隐藏标题栏 --> 
  <item name="android:windownotitle">true</item> 
</style> 

方法四:动态显示隐藏状态栏

//隐藏状态栏 
 windowmanager.layoutparams lp = context.getwindow().getattributes(); 
 lp.flags |= windowmanager.layoutparams.flag_fullscreen; 
 context.getwindow().setattributes(lp); 
//显示状态栏 
  windowmanager.layoutparams attr = context.getwindow().getattributes(); 
  attr.flags &= (~windowmanager.layoutparams.flag_fullscreen); 
  context.getwindow().setattributes(attr); 

方法五:动态显示隐藏状态栏

      view类提供了setsystemuivisibility和getsystemuivisibility方法,这两个方法实现对状态栏的动态显示或隐藏的操作,以及获取状态栏当前可见性。

setsystemuivisibility方法传入的实参分析:

   setsystemuivisibility(int visibility)方法可传入的实参为:

    1. view.system_ui_flag_visible:显示状态栏,
activity不全屏显示(恢复到有状态的正常情况)。
    2. view.invisible:隐藏状态栏,同时activity会伸展全屏显示。
    3. view.system_ui_flag_fullscreen:activity全屏显示,且状态栏被隐藏覆盖掉。
    4. view.system_ui_flag_layout_fullscreen:activity全屏显示,但状态栏不会被隐藏覆盖,状态栏依然可见,activity顶端布局部分会被状态遮住。
    5. view.system_ui_flag_layout_hide_navigation:效果同view.system_ui_flag_layout_fullscreen
    6. view.system_ui_layout_flags:效果同view.system_ui_flag_layout_fullscreen
    7. view.system_ui_flag_hide_navigation:隐藏虚拟按键(导航栏)。有些手机会用虚拟按键来代替物理按键。
    8. view.system_ui_flag_low_profile:状态栏显示处于低能显示状态(low profile模式),状态栏上一些图标显示会被隐藏。

package com.administrator.statubarapplication;  
  
import android.app.activity;  
import android.os.bundle;  
import android.view.view;  
import android.widget.button;  
import android.widget.linearlayout;  
import android.widget.textview;  
  
public class mainactivity extends activity implements view.onclicklistener {  
  
  @override  
  protected void oncreate(bundle savedinstancestate) {  
    super.oncreate(savedinstancestate);  
  
    setcontentview(r.layout.activity_main);  
    assignviews();  
  
  
    setonclicks();  
  }  
  
  private void setonclicks() {  
    mbutton1.setonclicklistener(this);  
    mbutton2.setonclicklistener(this);  
    mbutton3.setonclicklistener(this);  
    mbutton4.setonclicklistener(this);  
    mbutton5.setonclicklistener(this);  
    mbutton6.setonclicklistener(this);  
    mbutton7.setonclicklistener(this);  
  }  
  
  private linearlayout mmain;  
  private textview mtextview;  
  private button mbutton1;  
  private button mbutton2;  
  private button mbutton3;  
  private button mbutton4;  
  private button mbutton5;  
  private button mbutton6;  
  private button mbutton7;  
  
  private void assignviews() {  
    mmain = (linearlayout) findviewbyid(r.id.main);  
    mtextview = (textview) findviewbyid(r.id.textview);  
    mbutton1 = (button) findviewbyid(r.id.button1);  
    mbutton2 = (button) findviewbyid(r.id.button2);  
    mbutton3 = (button) findviewbyid(r.id.button3);  
    mbutton4 = (button) findviewbyid(r.id.button4);  
    mbutton5 = (button) findviewbyid(r.id.button5);  
    mbutton6 = (button) findviewbyid(r.id.button6);  
    mbutton7 = (button) findviewbyid(r.id.button7);  
  }  
  
  
  @override  
  public void onclick(view v) {  
    switch (v.getid()) {  
      case r.id.button1:  
        //activity全屏显示,且状态栏被隐藏覆盖掉  
        mmain.setsystemuivisibility(view.system_ui_flag_fullscreen);  
        mtextview.settext("activity全屏显示,且状态栏被隐藏覆盖掉\nview.system_ui_flag_fullscreen");  
        break;  
  
      case r.id.button2:  
        mmain.setsystemuivisibility(view.system_ui_flag_visible);  
        mtextview.settext("恢复到有状态的正常情况\nview.system_ui_flag_visible");  
        break;  
      case r.id.button3:  
        mmain.setsystemuivisibility(view.invisible);  
        mtextview.settext("//隐藏状态栏,同时activity会伸展全屏显示\nview.invisible");  
        break;  
      case r.id.button4:  
        mmain.setsystemuivisibility(view.system_ui_flag_layout_fullscreen);  
        mtextview.settext("activity全屏显示,但状态栏不会被隐藏覆盖,状态栏依然可见,activity顶端布局部分会被状态遮\nview" +  
            ".system_ui_flag_layout_fullscreen \n view.system_ui_flag_layout_hide_navigation");  
        break;  
      case r.id.button5:  
        mmain.setsystemuivisibility(view.system_ui_flag_layout_fullscreen);  
        mtextview.settext("activity全屏显示,状态栏透明\nview.system_ui_flag_layout_hide_navigation");  
        break;  
      case r.id.button6:  
        mmain.setsystemuivisibility(view.system_ui_flag_hide_navigation);  
        mtextview.settext("隐藏虚拟按键\nview.system_ui_flag_hide_navigation");  
        break;  
      case r.id.button7:  
        mmain.setsystemuivisibility(view.system_ui_flag_low_profile);  
        mtextview.settext("状态栏低能显示,有一些会被隐藏\nview.system_ui_flag_low_profile");  
        break;  
  
            default:  
        break;  
    }  
  }  
}