我的WebView没有填充选项卡的屏幕(但填充手机的屏幕没有pb)

时间:2011-07-20 13:33:51

标签: android-layout android-manifest android-webview android

我的应用需要实例化显示网站页面内容的WebView

它与手机完美配合(我已尝试使用各种HTC和最近的三星)但它不适用于平板电脑(Galaxy Tab或华硕) Eee Pad Transformer):WebView显示非常小,而不是填充屏幕。

根据选项卡,大约25%到50%的视口已填充,其余为空白。看起来好像我使用的是WRAP_CONTENT而不是FILL_PARENT

我正在使用的布局如下:

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

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/> 

使用此布局的活动的onCreate()如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.showdoc); // showdoc is the layout above

    // Configuration of the WebView
    final WebView webview = (WebView) findViewById(R.id.webview);
    final WebSettings settings = webview.getSettings();

    settings.setJavaScriptEnabled(true);

    // Callbacks setup 
    final Context ctx = this;

    webview.setWebViewClient(new WebViewClient() {
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Toast.makeText(ctx, getResources().getString(R.string.error_web) + description, Toast.LENGTH_LONG).show();
      }
    });

    // Display of the page
    webview.loadUrl(getResources().getString(R.string.doc_url) + Locale.getDefault().toString());
}

清单文件如下所示:

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

    <uses-permission android:name="android.permission.INTERNET" />

    <application android:icon="@drawable/icon" 
                 android:label="@string/app_name" 
                 android:debuggable="true">

    <activity android:name=".MainActivity"
              android:label="@string/app_name"
              android:configChanges="keyboardHidden|orientation">
        <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> 

当我使用常规浏览器访问相同的URL时,页面会正确填充屏幕。

下面的截图是使用三星Galaxy Tab 10 in。模拟器制作的,但结果在物理Eee Pad Transformer上基本相同,WebView小50%。在手机上没问题,WebView占用了所有可用空间。

我的应用,横向模式: My app, in landscape mode

我的应用,以纵向模式: My app, in portrait mode

相同的网址,相同的硬件,使用默认浏览器,处于横向模式: Same URL, same hardware, with the default browser, in landscape mode

相同的网址,相同的硬件,使用默认浏览器,处于纵向模式: Same URL, same hardware, with the default browser, in portrait mode

所以一定是设置问题。

我做了多少非常好的尝试:

  • 强制使用LayoutParams

我最初认为这是一个LayoutParams问题,因此我尝试将以下内容添加到onCreate(),但没有成功。它只是没有改变任何东西:

// Zoom out
final Display display = getWindowManager().getDefaultDisplay(); 

final int width = display.getWidth();
final int height = display.getHeight();

webview.setLayoutParams(new LayoutParams(width, height));
  • setScrollBarStyle()

我还查看了this discussion,然后尝试webview.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);,但也没有成功。

  • 使用LinearLayout

我已尝试将Layout更改为您的反馈意见(见下文),但不会改变任何内容:

<?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="fill_parent"
    /> 
</LinearLayout>
你知道吗? 提前谢谢!

3 个答案:

答案 0 :(得分:3)

我的猜测是它在平板电脑的兼容模式下运行。检查清单文件。你能发布你所看到的截图吗?

我怀疑是对的。它以兼容模式运行。

<supports-screens android:largeScreens="true"
android:normalScreens="true" android:smallScreens="true"
android:anyDensity="true" android:xlargeScreens="false" />

将这段代码放在<manifest>元素中,就在清单中的<application>标记之前。

答案 1 :(得分:0)

尝试将您的网页视图放在Linearlayout中,并使Linearlayout和webview的高度属性都为android:layout_height="match_parent"

答案 2 :(得分:0)

创建webView的新实例, KEY 是将webview设置为为我工作的contentview setContentView(webView);!希望它有所帮助。

public class DisplayWebPage extends Activity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);

    Intent intent = getIntent(); // Get the message from the intent
    String postString = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    String url = ("http://api.myDomain.com/sbmRank/app/1/CSS-Slider-Menu.php?action=" + postString);

    webView = new WebView(this);
    setContentView(webView);  //HERE IS THE KEY PART
    WebSettings webSettings = webView.getSettings();
    webSettings.setBuiltInZoomControls(true);

    webView.setWebViewClient(new Callback());
    webView.loadUrl(url);

}

   private class Callback extends WebViewClient{

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return (false);
        }

    }
相关问题