Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > android 标题栏状态栏

Android中隐藏状态栏和标题栏的方法汇总(隐藏状态栏、标题栏的五种方法)

作者:Billy_Zuo

这篇文章主要介绍了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中隐藏状态栏和标题栏的方法汇总(隐藏状态栏、标题栏的五种方法),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

您可能感兴趣的文章:
阅读全文