Android 完全隐藏状态栏方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 完全隐藏状态栏方法相关的知识,希望对你有一定的参考价值。
参考技术A android 完全隐藏状态栏方法 https://blog.csdn.net/ljbphoebe/article/details/881795761. 隐藏ActionBar:
ActionBar actionBar = getActionBar();
if (actionBar != null)
actionBar.hide();
如果是继承AppCompatActivity,就用getSupportActionBar()。
2. 隐藏状态栏
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
通过这两个步就可以全屏显示启动页了。
然而,当开始动态申请权限,弹出系统的权限提示对话框后,状态栏又重新露出来了。我日,不是隐藏了吗?怎么又出来了,什么鬼?
通过查看源码的解释:
/**
* Request that the visibility of the status bar or other screen/window
* decorations be changed.
*
* <p>This method is used to put the over device UI into temporary modes
* where the user's attention is focused more on the application content,
* by dimming or hiding surrounding system affordances. This is typically
* used in conjunction with @link Window#FEATURE_ACTION_BAR_OVERLAY
* Window.FEATURE_ACTION_BAR_OVERLAY, allowing the applications content
* to be placed behind the action bar (and with these flags other system
* affordances) so that smooth transitions between hiding and showing them
* can be done.
*
* <p>Two representative examples of the use of system UI visibility is
* implementing a content browsing application (like a magazine reader)
* and a video playing application.
*
* <p>The first code shows a typical implementation of a View in a content
* browsing application. In this implementation, the application goes
* into a content-oriented mode by hiding the status bar and action bar,
* and putting the navigation elements into lights out mode. The user can
* then interact with content while in this mode. Such an application should
* provide an easy way for the user to toggle out of the mode (such as to
* check information in the status bar or access notifications). In the
* implementation here, this is done simply by tapping on the content.
*
* @sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
* content
*
* <p>This second code sample shows a typical implementation of a View
* in a video playing application. In this situation, while the video is
* playing the application would like to go into a complete full-screen mode,
* to use as much of the display as possible for the video. When in this state
* the user can not interact with the application; the system intercepts
* touching on the screen to pop the UI out of full screen mode. See
* @link #fitSystemWindows(Rect) for a sample layout that goes with this code.
*
* @sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
* content
*
* @param visibility Bitwise-or of flags @link #SYSTEM_UI_FLAG_LOW_PROFILE,
* @link #SYSTEM_UI_FLAG_HIDE_NAVIGATION, @link #SYSTEM_UI_FLAG_FULLSCREEN,
* @link #SYSTEM_UI_FLAG_LAYOUT_STABLE, @link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION,
* @link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN, @link #SYSTEM_UI_FLAG_IMMERSIVE,
* and @link #SYSTEM_UI_FLAG_IMMERSIVE_STICKY.
*/
从释义上可以知道,setSystemUiVisibility()是用于使系统UI进入一种临时的模式,目的是使用户的注意力关注于应用程序的内容上。所以单独一个Activity这样设置是可以全屏显示的,这个只对当前的Activity有效。可是当申请系统权限使,弹出的对话框是系统的Activity,通过adb shell dumpsys activity 来看,当前最顶端的Activity已经是GrantPermissionsActivity。
Run #2: ActivityRecord2b99111 u0 com.google.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity t141
而这个GrantPermissionsActivity,我们并无法去设置它的setSystemUiVisibility()。所以这种方法不奏效。
通过和同事讨论,后来找到一种方法,可以实现我们的需求。
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
这种方法是OK的。
它的源码释义是:
/**
* Set the flags of the window, as per the
* @link WindowManager.LayoutParams WindowManager.LayoutParams
* flags.
*
* <p>Note that some flags must be set before the window decoration is
* created (by the first call to
* @link #setContentView(View, android.view.ViewGroup.LayoutParams) or
* @link #getDecorView():
* @link WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN and
* @link WindowManager.LayoutParams#FLAG_LAYOUT_INSET_DECOR. These
* will be set for you based on the @link android.R.attr#windowIsFloating
* attribute.
*
* @param flags The new window flags (see WindowManager.LayoutParams).
* @param mask Which of the window flag bits to modify.
* @see #addFlags
* @see #clearFlags
*/
public void setFlags(int flags, int mask)
仔细分析发现,这个是设置整个当前Window的,而setSystemUiVisibility()聚焦于显示Activity内容的,还是有差别的。
/**
* Window flag: hide all screen decorations (such as the status bar) while
* this window is displayed. This allows the window to use the entire
* display space for itself -- the status bar will be hidden when
* an app window with this flag set is on the top layer. A fullscreen window
* will ignore a value of @link #SOFT_INPUT_ADJUST_RESIZE for the window's
* @link #softInputMode field; the window will stay fullscreen
* and will not resize.
*
* <p>This flag can be controlled in your theme through the
* @link android.R.attr#windowFullscreen attribute; this attribute
* is automatically set for you in the standard fullscreen themes
* such as @link android.R.style#Theme_NoTitleBar_Fullscreen,
* @link android.R.style#Theme_Black_NoTitleBar_Fullscreen,
* @link android.R.style#Theme_Light_NoTitleBar_Fullscreen,
* @link android.R.style#Theme_Holo_NoActionBar_Fullscreen,
* @link android.R.style#Theme_Holo_Light_NoActionBar_Fullscreen,
* @link android.R.style#Theme_DeviceDefault_NoActionBar_Fullscreen, and
* @link android.R.style#Theme_DeviceDefault_Light_NoActionBar_Fullscreen.</p>
*/
public static final int FLAG_FULLSCREEN = 0x00000400;
从释义上得知,设置这个flag可以隐藏所有的屏幕修饰,像status bar,用于Window使用整个显示屏。这个完全是我们的目的了。
Android全屏(包含3种隐藏顶部状态栏及标题栏和一种隐藏Android 4.0平板底部状态栏的方法)
方法一
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.0以上系统的平板的屏幕下方的状态栏 */ try { String ProcID = "79"; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) ProcID = "42"; // ICS // 需要root 权限 Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "service call activity " + ProcID + " s16 com.android.systemui" }); // WAS proc.waitFor(); } catch (Exception ex) { Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_LONG).show(); } } @Override protected void onDestroy() { // TODO Auto-generated method stub /* * 恢复运行Android 4.0以上系统的平板的屏幕下方的状态栏 */ try { Process proc = Runtime.getRuntime().exec(new String[] { "am", "startservice", "-n", "com.android.systemui/.SystemUIService" }); proc.waitFor(); } catch (Exception e) { e.printStackTrace(); } super.onDestroy(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.startup, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.action_exit: finish(); break; } return true; } }
由于没有了状态栏,须在程序中提供退出程序的方法。
http://www.xuebuyuan.com/558284.html
以上是关于Android 完全隐藏状态栏方法的主要内容,如果未能解决你的问题,请参考以下文章
Android - 为带有导航抽屉和应用栏的应用完全隐藏状态和导航栏