Strange behaviour when using WebView and RelativeLayout in fullscreen

EDIT: Here's the video showing the problem: www.youtube.com/watch?v=5ZZsuMH5T9k I'm really stuck here :/

--

I have an activity which consists of a webview and a button at the bottom. I set the window of the activity to fullscreen using this code in onCreate :

@Override
public void onCreate(Bundle savedInstanceState) {
  InstaFetchApplication.applyCurrentTheme(this);

  super.onCreate(savedInstanceState);

  getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  requestWindowFeature(Window.FEATURE_PROGRESS);

  if (UserPreferences.getIsFullScreenReadingEnabled(this)) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    // this just do this: window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    ActivityUtils.toggleFullScreen(this, true);
  }

  setContentView(R.layout.view_article);

  // ...
}

The problem is that most of the time the webview seems to "push" the button off the screen. Here's the screenshot:

(larger version)

What I would expect to see (and sometimes I do) is here:

(larger version)

Here's the layout I'm using (note that in my app the top layout has to be a RelativeLayout):

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/layout_test"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
>
  <Button
    android:id="@+id/button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="Click me!"
  />
  <WebView
    android:id="@+id/web_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/button"
  />
</RelativeLayout>

I also noticed that if you turn off all animations on the device using system settings, the issue is gone (though there's this glitch when the whole view has a top margin and then it jumps back in the correct position).

Did anyone encounter similar problem and know how to fix it?


Finally I found the answer. Adding WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS flag to the window solved my problem.

See this post: flag_fullscreen + windowNoTitle == button focus bug.


I just added a button to the Hello WebView tutorial, and everything seems to look right. Then I converted main.xml from LinearLayout to RelativeLayout , and everything still seems to look right. I think you are making things too complicated. :-) Try removing most of that initialization code from onCreate() and just specify it in the XML.

Here is my project.

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.hellowebview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".HelloWebViewActivity"
            android:label="@string/app_name" 
            android:theme="@android:style/Theme.NoTitleBar"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

HelloWebViewActivity.java:

package com.example.android.hellowebview;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class HelloWebViewActivity extends Activity {

    WebView mWebView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_rl);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.setWebViewClient(new HelloWebViewClient());
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://www.google.com");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}

main_ll.xml (LinearLayout):

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

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
    />
    <Button
        android:id="@+id/mainbtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="@string/btn_label"
    />
</LinearLayout>

main_rl.xml (RelativeLayout):

<?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"
    >
    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/mainbtn"
    />
    <Button
        android:id="@+id/mainbtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="@string/btn_label"
    />
</RelativeLayout>

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, HelloWebViewActivity!</string>
    <string name="app_name">HelloWebView</string>
    <string name="btn_label">Push Me</string>

</resources>

Try to align the web view to the parent top, and align the button to the parent bottom and to be below the web view. I have found times when, for no real reason, the elements don't quite line up correctly if they are not all strung together in a linear sequence either anchored at the top or at the bottom of the screen.

I presume that you are not overriding the onMeasure() method and changing the measured height of the button. I've created numerous (too many) bugs during development of my apps by miscalculating the height of an element during the layout phase and have spent hours struggling with debugging the layout definition code only to find that it was fine, but the height calculation was feeding invalid data into the layout's rendering phase.

链接地址: http://www.djcxy.com/p/56846.html

上一篇: 为什么“cd”不能在shell脚本中工作?

下一篇: 在全屏中使用WebView和RelativeLayout时出现奇怪的行为