Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 网络视图大小始终为0_Android_Android Webview - Fatal编程技术网

Android 网络视图大小始终为0

Android 网络视图大小始终为0,android,android-webview,Android,Android Webview,我似乎无法显示我的WebView。我怀疑是因为它的尺寸是0。我不知道如何取消此网络视图的零 这是我的布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <WebView android:i

我似乎无法显示我的WebView。我怀疑是因为它的尺寸是0。我不知道如何取消此网络视图的零

这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

通过使用
Activity.setContentView()
将布局附加到应用程序窗口,您正在扩展布局,但从未显示它。使用其中一个

View webViewLayout = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
        .inflate(R.layout.twitterfeed, null, false);
setContentView(webViewLayout);
WebView webView = (WebView) webViewLayout.findViewById(R.id.webView1);
或者更简单地说,让框架为您实现通胀:

setContentView(R.layout.twitterfeed);
WebView webView = (WebView) findViewById(R.id.webView1);
  • 您需要使用ViewTreeObserver来测量webview布局的宽度和高度
  • ViewTreeObserver触发它的全局布局侦听器,并有自己的线程来测量布局维度

            webView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
        {
            @Override
            public void onGlobalLayout()
            {
                System.out.println("webView: " + webView.getWidth());
                System.out.println("webView: " + webView.getHeight());
            }
        });
    

当我尝试设置ContentView()时,出现了以下异常:
java.lang.RuntimeException:您的内容必须具有id属性为“android.R.id.list”的ListView
。这似乎很奇怪。为什么我必须有一个ListView?您正在扩展
ListActivity
。将其更改为常规
活动
        webView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
    {
        @Override
        public void onGlobalLayout()
        {
            System.out.println("webView: " + webView.getWidth());
            System.out.println("webView: " + webView.getHeight());
        }
    });