1. 程式人生 > >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;  
        }  
    }  
}