读书人

[]嵌在RelativeLayout中的WebView无法

发布时间: 2012-05-28 17:59:33 作者: rapoo

[求助]嵌在RelativeLayout中的WebView无法显示内容!
目的:在学习自定义控件过程中,想把一个WebView嵌入到RelativeLayout中,然后使用loadURL加载某URL!
实现:

package com.study.test;

public class myWebView extends RelativeLayout
{
public myWebView(Context context)
{
this(context, null, 0);
}

public myWebView(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}

public myWebView(Context context, AttributeSet attrs, int defStyle)
{
if (uiThreadHandler == null)
{
uiThreadHandler = new Handler();
}

new Thread()
{
public void run()
{
try
{
// Get a URL from server.
Context context = getContext();
String newURL = GetNewURL(context);

if (newURL != null) // we get an URL back
{
synchronized(this)
{
// Create a view for the URL.

Looper.prepare();
final WebView newWebView = new WebView ( context);

LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, 48);
newWebView.setLayoutParams(params);
newWebView.loadUrl(newURL );

// Force this view to show the webview
uiThreadHandler.post(new Runnable()
{
public void run()
{
try
{
addView(newWebView);
}
catch (Exception e)
{

}
finally
{

}
}
});
}
}
}
}
catch (Exception e)
{

}
}
}.start();
}
}


在应用的main.xml添加如下内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<com.study.test.myWebView
android:id="@+id/myWeb"
android:layout_width="fill_parent"


android:layout_height="wrap_content"
/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>

</LinearLayout>


现象:获取URL后,WebView没有显示任何内容!
但是如果把webView换成TextView,可以显示URL的,百思不得其解,还望高手帮忙分析,谢谢!

[解决办法]
extends RelativeLayout
去掉呢?

还有,你为什么要在程序中来extern这个layout,你可以在xml中把webview用releativlayout包含起来啊。
[解决办法]

Java code
package com.webview;import android.app.Activity;import android.os.Bundle;import android.view.Window;import android.webkit.WebView;public class WebViewActivity extends Activity {        /** Called when the activity is first created. */    @Override        public void onCreate(Bundle savedInstanceState) {        WebView webview;        super.onCreate(savedInstanceState);        getWindow().requestFeature(Window.FEATURE_PROGRESS);        setContentView(R.layout.main);        webview = (WebView) findViewById(R.id.webview);          webview.getSettings().setJavaScriptEnabled(true);          webview.loadUrl("http://www.google.com");    }    }
[解决办法]
换个 WebView,我用的是com.github.droidfu.widgets.WebImageView这个

读书人网 >Android

热点推荐