温馨提示×

温馨提示×

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

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

Android应用怎么实现隐藏状态栏

发布时间:2020-11-21 16:27:09 来源:亿速云 阅读:1681 作者:Leah 栏目:移动开发

这篇文章将为大家详细讲解有关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;  
    }  
  }  
} 

下面看下 隐藏标题栏和底部操作栏,可上下滑动显示代码

@Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
if (hasFocus)
{
View decorView = that.getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN);
}
}

关于Android应用怎么实现隐藏状态栏就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

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

AI