2011-12-18 62 views
7

編輯使用的WebView和RelativeLayout的時候: 這裏的顯示問題視頻:www.youtube.com/watch?v=5ZZsuMH5T9k我真的被困在這裏:/奇怪的行爲在全屏

-

我有它由一個活動一個webview和一個按鈕在底部。我設置了活動的窗口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); 

    // ... 
} 

的問題是,大部分時間 web視圖似乎「推」按鈕關閉屏幕。下面是截圖:

wrong

larger version

我希望看到(和有時我)在這裏:

correct

larger version

這裏是

<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> 

我也注意到,如果你使用的系統設置中關閉所有的動畫在設備上,這個問題就沒了(:我使用的(請注意,我的應用程序上的佈局已經是一個RelativeLayout的)佈局雖然當整個視圖有一個頂部邊距然後它跳回到正確的位置時會出現這個故障)。

有沒有人遇到類似的問題,並知道如何解決它?

+1

你可以發佈'onCreate()' – 2011-12-21 05:22:18

+0

你正在測試設備或模擬器? – 2011-12-21 06:49:49

+0

你可以發佈你的代碼嗎?標題欄中是否需要進度對話框? 'Window.FEATURE_PROGRESS'應該和'setProgressBarVisibility(true)'一起使用,或者如果你不想使用進度條,就不要使用它。如果你想隱藏標題欄,那麼你可以使用'android:windowNoTitle' – 2011-12-21 17:22:44

回答

0

除非RelativeLayout是絕對必要的,你可以更改爲LinearLayout

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <WebView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="I'm a button"/> 
</LinearLayout> 

它應該給一個更好的結果。

+0

謝謝,但它確實必須是RelativeLayout。 – 2011-12-21 20:29:20

+1

而不是指定機制,爲什麼不指定你需要達到的結果。你能解釋一下設計約束是否會導致非RelativeLayout解決方案不可行? – Sparky 2011-12-21 20:44:26

+0

如果將WebView設置爲alignParentTop而不是layout_above,它會工作嗎?該視圖將能夠延伸到按鈕後面,而不是將它推下... – Jave 2011-12-22 09:39:46

0

要隱藏我用的標題欄:

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

測試上的Galaxy S 2(安卓2.3.3)和華爲U8180 X1(安卓2.2.2),並與相對佈局工作的罰款。

從屏幕截圖中可以看出問題在於您所要求的進度指示器不完整。關閉動畫時問題消失的事實也支持這一理論。

+0

謝謝,實際上我使用的setFlags與FLAG_FULLSCREEN只是沒有在錯誤中將它包含在問題中。我嘗試刪除FEATURE_PROGRESS,但它沒有幫助。 – 2011-12-21 20:30:10

+0

什麼版本的Android和您正在測試什麼設備? 你有沒有看到LinearLayout的奇怪行爲?我仍然相信壞屏幕上的黑色方塊與佈局沒有任何關係,但卻是一半的標題或通知欄。 – 2011-12-22 13:01:46

0

你可以試試這個。頂部佈局仍然RelativeLayout,用你的唯一的區別是在LinearLayout中結束了一切:

<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" 

> 
<LinearLayout android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:weightSum="1"> 
    <WebView 

     android:id="@+id/web_view" 
     android:layout_width="fill_parent" 
     android:layout_height="0px" 
     android:layout_weight=".90" /> 


</LinearLayout> 
</RelativeLayout> 

LinearLayoutWebView佔據整個窗口(和父母)的90%和按鈕剩餘10%。兩者的高度都設置爲0px,所以重量可以爲他們兩人完成工作。解析這個XML的代價是微不足道的。

而且在Java代碼中我所做的:

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 


     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     requestWindowFeature(Window.FEATURE_PROGRESS); 
     setContentView(R.layout.main); 
     LinearLayout mLinearLayout = (LinearLayout)findViewById(R.id.ll); 
     Button mButton = new Button(this); 
     LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0, 0.10f); 
     mButton.setText("Click me!"); 
     mButton.setLayoutParams(mParams); 
     mLinearLayout.addView(mButton); 
     mLinearLayout.invalidate(); 

    } 
1

我只是增加了一個按鈕,Hello WebView教程,一切似乎看的權利。然後我將main.xmlLinearLayout轉換爲RelativeLayout,並且所有東西看起來都看起來不錯。我認爲你讓事情太複雜了。 :-)嘗試從onCreate()中刪除大部分初始化代碼,並在XML中指定它。

這是我的項目。

的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> 
+0

對於相對佈局android:layout_above =「@ + id/mainbtn」將不起作用,因爲您在該代碼下面定義了buttion – Dharmendra 2011-12-23 05:27:04

+0

我不能說它是否可以工作,但它確實有效。我在運行Android 1.6,2.3和4.0的設備上進行了測試。 – Sparky 2011-12-23 18:11:31

1

嘗試對齊web視圖到父頂部,並對齊按鈕到母體底部是web視圖下方。我發現,如果沒有真正的原因,如果元素不是以線性順序排列在一起,而是固定在屏幕的頂部或底部,那麼這些元素的排列不正確。

我認爲你不是重寫onMeasure()方法,並改變按鈕的測量高度。在開發我的應用程序時,我通過在佈局階段錯誤計算元素的高度而創建了大量(太多)的錯誤,並且花費了幾個小時來苦苦尋找佈局定義代碼,以發現它很好,但高度計算是將無效數據提供給佈局的渲染階段。

+0

看來,這不是佈局錯位的問題。就好像框架認爲由於通知欄的動畫消失,整個窗口更小。 – 2012-01-18 15:28:22

0

請參考以下鏈接:

http://marakana.com/forums/android/examples/58.html

下載項目文件(.zip)。

導入它,讓我知道這個項目是否有任何幫助。

+0

我不明白這會有什麼幫助。我的情況與您在項目中的情況完全不同。 – 2012-01-18 15:26:03

+0

在這個項目中,編輯文本和按鈕位於webView的上方,只需在webView下面調整它們:) – 2012-01-19 09:42:31

0

下面的代碼可能會幫助你,

webView = (WebView) findViewById(R.id.webview1); 
    webView.setInitialScale(100); 

    webView.setWebViewClient(new WebViewClient() { 
     @Override 
     public void onPageFinished(WebView view, String url) { 
      // TODO Auto-generated method stub 
      super.onPageFinished(view, url); 
      view.scrollTo(0, 0); 

     } 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 

      view.loadDataWithBaseURL(null, yourdata, "text/html", "utf-8", null); 
      // view.loadUrl(url); 
      return true; 
     } 
    }); 


webView.setWebChromeClient(new WebChromeClient() { 
    @Override 
    public void onProgressChanged(WebView view, int newProgress) { 
     try { 
      //your stuff 
     } catch (Throwable e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

不,這沒有幫助。 – 2012-01-18 15:24:03