Android全屏(包含3種隱藏頂部狀態欄及標題欄和一種隱藏Android 4.0平板底部狀態欄的方法)

http://www.xuebuyuan.com/558284.html

方法一

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>

注:

1、方法一中的兩段代碼要在setContentView()之前。

2、方法二隻能同時隱藏狀態欄和標題欄。

3、方法一和方法二都只應用於單個Activity。方法三應用於整個程序

對於運行Android 4.0以上系統的平板電腦,以上三種方法都不會隱藏屏幕下方的狀態欄,須做如下處理。

複製代碼
public class StartupActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_startup);

        /*
         * 隱藏運行Android 4.xxx系統的平板的屏幕下方的狀態欄需要root權限
         */
        closeBar();
    }

    @Override
    protected void onDestroy() {
        showBar();
        super.onDestroy();
    }

    /**
     * 關閉Android導航欄,實現全屏
     */
    private void closeBar() {
        try {
            String command;
            command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib service call activity 42 s16 com.android.systemui";
            ArrayList<String> envlist = new ArrayList<String>();
            Map<String, String> env = System.getenv();
            for (String envName : env.keySet()) {
                envlist.add(envName + "=" + env.get(envName));
            }
            String[] envp = envlist.toArray(new String[0]);
            Process proc = Runtime.getRuntime().exec(
                    new String[] { "su", "-c", command }, envp);
            proc.waitFor();
        } catch (Exception ex) {
            // Toast.makeText(getApplicationContext(), ex.getMessage(),
            // Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 顯示導航欄
     */
    public static void showBar() {
        try {
            String command;
            command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib am startservice -n com.android.systemui/.SystemUIService";
            ArrayList<String> envlist = new ArrayList<String>();
            Map<String, String> env = System.getenv();
            for (String envName : env.keySet()) {
                envlist.add(envName + "=" + env.get(envName));
            }
            String[] envp = envlist.toArray(new String[0]);
            Process proc = Runtime.getRuntime().exec(
                    new String[] { "su", "-c", command }, envp);
            proc.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
複製代碼

 

由於沒有了狀態欄,須在程序中提供退出程序的方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章