Android全屏WebView

3
我的应用程序中的 WebView 显示为窗口,但我希望它是全屏的!这是屏幕截图: not fullscreen 以下是我的代码:
public class DockViewerActivity extends Activity {

    private WebView mWebView;

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

        mWebView = new WebView(this);
        setContentView(mWebView);
        mWebView.loadUrl("file:///android_asset/1.html");

        Toast.makeText(this, getString(R.string.loading), Toast.LENGTH_LONG).show();
    }
}

尝试一下这个代码:getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); - M D
不起作用。得到相同的结果。 - Coma White
您的活动已经全屏,确保您的 Webview 填充宽度和高度。 - elmorabea
试试这个:mWebView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); - Shivam Verma
2个回答

4

XML

请将高度和宽度设置为match_parent,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/conrainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

    <WebView
         android:id="@+id/webview"
         android:layout_width="match_parent "
         android:layout_height="match_parent "/>

</RelativeLayout>

Activity::

要移除状态栏,您需要使用以下方法:

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

要移除标题栏,您需要使用:

requestWindowFeature(Window.FEATURE_NO_TITLE); 

在设置内容视图之前,您需要设置以上两个功能,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.base_screen);
}

如果您通过编程方式创建webView,则应将heightwidth设置为match_parent,如下所示:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
 mWebView .setLayoutParams(layoutParams);

现在已经完成了! #庆祝!

1

以下是我编写的代码,可以实现全屏Web视图。

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
        setContentView(R.layout.activity_main);

    myWebView = (WebView)findViewById(R.id.webview);
            ...
    }

这个活动的 XML 布局如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mains"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
    ...
    <WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible"
/>
    ...
</RelativeLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接