如何显示活动内的网页视图在屏幕的中间

如何显示活动内的网页视图在屏幕的中间

本文介绍了如何显示活动内的网页视图在屏幕的中间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果我遇到的问题,同时显示在屏幕的中间在活动的WebView。
我有一个活动,我想在显示屏幕中心网页视图。我的活动是透明的,所以后台活动将是可见的。每当我尝试创建一个web视图并使用的setContentView(web视图),将其添加到活动它总是显示在屏幕的左上角的视图。是他们的方式来解决这个?我试图通过纯$ C $这样做仅C。这里是我的code


I am having issue while showing a WebView in an activity in the middle of the screen.I have an activity and I want to show a webview in the center of screen. My activity is transparent so background activity will be visible. Whenever I try to create a webview and add it to activity using setContentView(webview) it always shows the view on the top left corner of the screen. Is their a way to workaround this?I am trying to do this via pure code only. Here is my code.

protected void onCreate(Bundle savedInstance) {
    //some initialization stuff
    LinearLayout ll = new LinearLayout(activity);
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.setGravity(Gravity.CENTER_HORIZONTAL);
    ll.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                        ViewGroup.LayoutParams.FILL_PARENT));
    ll.setBackgroundColor(android.R.color.transparent);
    ll.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL); //added after suggestion
    webView = new WebView(this);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.loadUrl("http://www.google.com");
    ll.addView(webView);
    webView.setBackgroundColor(android.R.color.transparent);
    setContentView(ll);
}

推荐答案

的setContentView(的WebView)是不可能的,你确定你在这里传递web视图作为参数?它应该是一个id到布局资源。还是你的意思 addView

setContentView(webview) is not possible, are you sure you're passing the webView here as param? It should be an id to a layout resource. Or do you mean addView?

您应该放置在布局资源的WebView并添加layout_gravity属性,以

You should place the WebView in a layout resource and add the layout_gravity attributes, to

layout_gravity="center_horizontal|center_vertical"

和/或周围的LinearLayout或任何你使用:

and/or the surrounding linearLayout or whatever you use to:

gravity="center_horizontal|center_vertical"

请更多的澄清提供您code。

Please provide your code for more clarification.

这篇关于如何显示活动内的网页视图在屏幕的中间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:31