我想以编程方式将WebView
添加到我的Activity
。这是我的代码:
public class MainActivity extends Activity {
private WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
wv = new WebView(this);
wv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
linearLayout.addView(wv);
super.setContentView(linearLayout);
}
@Override
protected void onResume() {
super.onResume();
wv.loadUrl("www.google.com");
}
}
这是AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testing.kaarelp.learn">
<application>
<uses-permission android:name="android.permission.INTERNET" />
<activity android:name=".pakk.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
但是当我打开应用程序时,我得到一个空白的白色屏幕。为什么它没有显示www.google.com?
-------------编辑2 --------------
问题不是将https://
或(http://
)放在www.google.com
前面。所以它应该是:
wv.loadUrl("https://www.google.com");
答案 0 :(得分:1)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView webView = (WebView) findViewById(R.id.fragment_about_us_wv);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.google.co.in/");
return view;
}
和xml一样
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:id="@+id/fragment_about_us_wv"
android:layout_width="match_parent"
android:background="@color/backGroundColor"
android:layout_height="match_parent" />
</LinearLayout>
答案 1 :(得分:1)
你的整个代码都没问题。您只需要使用http或https传递传递Web链接。
webview wv;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
linearlayout linearlayout = new linearlayout(this);
linearlayout.setorientation(linearlayout.vertical);
wv = new webview(this);
wv.setlayoutparams(new linearlayout.layoutparams(linearlayout.layoutparams.match_parent, linearlayout.layoutparams.match_parent));
linearlayout.addview(wv);
super.setcontentview(linearlayout);
}
@override
protected void onresume() {
super.onresume();
wv.loadurl("http://www.google.com");
}