Android全屏显示的问题(全屏BUG)

本文探讨了在Android开发中遇到的全屏显示问题,特别是在1.5和1.6版本中,当隐藏标题栏和状态栏后,界面会出现下移现象。作者发现这可能是Android的一个BUG,并提供了不同方法进行尝试,包括设置窗口特征和修改窗口类型。在某些版本中,全屏模式下按钮的触摸事件也会受到影响。文中还提到了社区中关于该问题的讨论和可能的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://hi.baidu.com/lynn185/item/87ff8c0a8f40d7066d90487d

近期刚刚接触到Android,由于原来是做Java的,再加上对嵌入式比较感兴趣的缘故,所以闲暇之余就搞搞Android的开发。

      众所周知,手机的界面相对较小,因此有时对界面进行布局设计的时候,总感觉不能如我们所愿。因此我们常常会想办法把屏幕的可用之处尽量变大,而去掉标题栏或者隐藏状态栏就是我们唯一的也是最好的选择。

测试环境:Eclipse 3.4+ADT 0.9+Android SDK 1.6

     在Android中去掉标题栏的方法如下2种:

         Java代码:requestWindowFeature(Window.FEATURE_NO_TITLE);

         XML配置文件:android:theme="@android:style/Theme.NoTitleBar"
     根绝个人的习惯,你可以选择在代码中进行隐藏,也可以在xml文件中进行隐藏。

     下面是去掉状态栏的方法:

         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,                       

                                       WindowManager.LayoutParams.FLAG_FULLSCREEN);
     如上所说,如果我们在程序中把两种方法都是用的话,就可以把状态栏和标题栏同时隐藏掉,这样就会达到我们想全屏的目的。

     一般的,我们全屏显示的原因就是想在屏幕上布局较多的界面元素,那么我们就来添加一些元素,首先我们来添加一个按钮,这时问题出现了,界面是全屏了,但是当我滑动鼠标或者点击导航键的时候,屏幕会整体下移一个相当于一个标题栏的高度。如下图红色部分即为下移后才显示出来的:

     我就很纳闷,后来经过我跟踪发现,假如标题栏或者状态栏只要显示任何其中一个,就不会出现界面下移的这种情况。我就又继续跟踪测试发现全屏模式下,添加其他组件都没有任何界面下移的情况,就只有当我试图添加一个按钮的时候,才回出现这种情况,因此我认为这是Android的一个BUG。果然,我继续在其它版本的SDK下测试了一下,结果发现1.5和1.6都会出现这种全屏后界面整体下移的问题,在2.0,2.0.1和2.1平台下不会出现上述问题。看样子是官方及时的修正了这个问题,就是不知道,在已经安装了1.5和1.6的手机中是否有这种补丁可以下载更正。否则的话,1.5和1.6的用户看样子是很难体验全屏的感觉了。好在现在网络发达,网上有很多刷机的教程,不懂计算机的人员也可以通过自己刷机来解决这个问题。

     那么在1.5和1.6平台真的就没有其他的方法可以正常的现实全屏了吗?

     非也!我们还可以通过另一种方法来实现我们喜欢的全屏的模式,下面就来看一下这种方法。

     隐藏状态栏代码:

     getWindow().setFlags(WindowManager.LayoutParams.TYPE_STATUS_BAR,

                                    WindowManager.LayoutParams.TYPE_STATUS_BAR);

    细心的朋友可能发现了,这行代码和上面隐藏状态栏的行数是一样的,只是里面的参数换了而已。经测试,在这种情况下,的确可以正常的显示界面,但是另一个问题又出现了,说明一下此次的测试环境是SDK1.6。这样实现全屏的话,我发现添加的按钮不会响应onTouch()事件,不过还可以响应onClick()事件,我认为是又一个BUG。其它的组件会不会响应以及在其他的SDK下触摸事件是不是修正了,我没有测试,感兴趣的朋友可以自己测试一下。


https://groups.google.com/forum/?fromgroups=#!topic/android-developers/LedwQ_MoNao

Hi guys,

I've found an irritating bug that I could please use some help with.

My application is styled to show no title (android:windowNoTitle) and
in onCreate() in the activity the system toolbar is hidden to display
as fullscreen. There are two buttons in the activity's relative
layout, aligned top of parent and bottom. The bug happens when I use 5-
way navigation to focus the top button. The screen momentarily shifts
or bounces down the size of the default titlebar. In some instances
part of the bottom content gets clipped. Here is a mini app I created
to reproduce the bug. Any help to workaround this bug would be
appreciated! Note: you may have to run the app twice to see this
behavior.

Brady

[bug.java]

package com.adobe.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.WindowManager;

public class StatusBarBug extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(

savedInstanceState);
                getWindow(). setFlags(WindowManager. LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams. FLAG_FULLSCREEN);

        setContentView(R.layout.main);
    }
}


[main.xml]

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=" http://schemas.android.com/apk/res/
android"
        android:layout_width=" fill_parent"
        android:layout_height= "fill_parent">

                <Button
                         android:layout_width="wrap_ content"
                         android:layout_height="wrap_ content"
                         android:text="button"/>

                <Button android:layout_width="wrap_ content"
                         android:layout_height="wrap_ content"
                         android:layout_ alignParentBottom="true"
                         android:text="button2">
                        < requestFocus/>
                </Button>

</RelativeLayout>


[styles.xml]

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <!-- Themes -->
  <style name="StatusBarTheme">
        <item name="android:windowNoTitle"> true</item>
  </style>
</resources>


[manifest.xml]

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=" http://schemas.android.com/apk/res/android"
      package="com.adobe.test"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/
app_name"
            android:theme="@style/ StatusBarTheme">
        <activity android:name=".StatusBarBug"
                  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>
    <uses-sdk android:minSdkVersion="3" />
</manifest>


This looks like a total hack but by adding this line

getWindow().setFlags(

WindowManager.LayoutParams. FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.
FLAG_LAYOUT_NO_LIMITS);

to the code I don't have the problem anymore.

This workaround kind of makes sense assuming something was being drawn
outside of the viewable bounds of the screen (like the titlebar, or
focus state of the button). The screen now won't shift to show
offscreen content because it can extend outside of the viewable screen
with this flag.

It still seems like a bug needs to be logged against this.

Brady

---------------------------------comment from forlong401--------------------------

可以考虑在每次屏幕切换或者view的显示、隐藏等操作时,再次调用一下:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

或者使用android:theme="@android:style/Theme.NoTitleBar.Fullscreen"代替

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);


这个问题在android4.0以后就不存在了。只是在android2.3,android2.2等机器上出现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值