Android开发

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

本文主要是介绍Android五种隐藏状态栏和标题栏的方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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;  
    }  
  }  
}  


这篇关于Android五种隐藏状态栏和标题栏的方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!