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

发布于 2024-11-25 15:16:50 字数 4401 浏览 1 评论 0原文

我的应用程序需要实例化一个 WebView 来显示网站页面的内容。

它与手机完美配合(我已经尝试过各种 HTC 和最近的三星)但它不适用于平板电脑(Galaxy Tab 或 Asus 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 时浏览器中,页面确实正确填满屏幕。

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

我的应用程序在横向模式下: 我的应用程序,处于横向模式

我的应用程序,处于纵向模式: 我的应用程序,纵向模式

相同的 URL,相同的硬件,使用默认浏览器,横向模式: 相同的 URL,相同的硬件,使用默认浏览器,横向模式

相同的 URL,相同的硬件,使用默认浏览器,在肖像模式: 相同的 URL,相同的硬件,使用默认浏览器,纵向模式

所以这一定是设置问题。

到目前为止我没有成功尝试过的:

  • 强制 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()

我还查看了 此讨论,然后尝试 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>

您有什么想法吗? 提前致谢!

My app needs to instantiate a WebView that displays the content of a page of a website.

It works perfectly with cellphones (I've tried with various HTC's and with a recent Samsung too) but it doesn't with tablets (Galaxy Tab, or Asus Eee Pad Transformer) : instead of filling the screen the WebView appears very small.

Depending of the tabs, roughly 25% to 50% of the viewport is filled, the rest is blank. It looks a bit as if I was using WRAP_CONTENT instead of FILL_PARENT.

The layout I'm using looks like this:

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

The onCreate() of the activity that uses this layout looks like this:

@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());
}

And the manifest file looks like this:

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

When I access to the same URL with the regular browser, the page does properly fill the screen.

The screenshot below were made using a Samsung Galaxy Tab 10 in. emulator, but the result is basically the same on a physical Eee Pad Transformer, the WebView being 50% smaller. On a cellphone, no problem, the WebView takes all the available room.

My app, in landscape mode:
My app, in landscape mode

My app, in portrait 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 landscape mode

Same URL, same hardware, with the default browser, in portrait mode:
Same URL, same hardware, with the default browser, in portrait mode

So it must be a matter of settings.

WHAT I HAVE UNSUCCESSFULLY TRIED SO FAR:

  • Forcing the LayoutParams

I initially thought it was a LayoutParams problem, so I tried adding what follows to my onCreate(), but with no success. It just doesn't change anything:

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

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

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

I also looked at this discussion and then tried webview.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY); , but with no success either.

  • Using a LinearLayout

I have tried changing the Layout as follows further to your feedback (see below), but it does not change anything:

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

Do you have any idea?
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

汹涌人海 2024-12-02 15:16:50

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

我的怀疑是正确的。它正在兼容模式下运行。

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

将此代码段放入 元素内,位于清单中 标记之前。

My guess is that it is running in compatibility mode for tablets. Check your manifest file. Could you post a screenshot of what you are seeing?

My suspicion was correct. It is running in compatibility mode.

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

Put this piece of code inside the <manifest> element, just before the <application> tag in your manifest.

笔落惊风雨 2024-12-02 15:16:50

尝试将您的 webview 放入 Linearlayout 中,并使 Linearlayout 和 webview 的高度属性都具有 android:layout_height="match_parent"

Try to put your webview in a Linearlayout and make both the height attributes of the Linearlayout and webview has android:layout_height="match_parent"

优雅的叶子 2024-12-02 15:16:50

创建一个新的 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);
        }

    }

create a new instance of the webView and the KEY is to set the webview to the contentview setContentView(webView); worked for me! hope it helps.

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);
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文