WebView取得外部網站於app內觀看 / fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<WebView
android:id="@+id/webView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
WebView取得外部網站於app內觀看 / Java
import
android.app.Activity;
import
android.app.Fragment;
import
android.os.Bundle;
import
android.view.LayoutInflater;
import
android.view.Menu;
import
android.view.MenuItem;
import
android.view.View;
import
android.view.ViewGroup;
import
android.webkit.WebSettings;
import
android.webkit.WebView;
import
android.webkit.WebViewClient;
public
class WebBrowerActivity extends Activity {
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_brower);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new
PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the
action bar if it is present.
getMenuInflater().inflate(R.menu.web_brower,
menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem
item) {
// Handle action bar item clicks here. The
action bar will
// automatically handle clicks on the
Home/Up button, so long
// as you specify a parent activity in
AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A
placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment
extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater
inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_web_brower,
container, false);
WebView
webView=(WebView)rootView.findViewById(R.id.webView01);
webView.setWebViewClient(new
CallBack());
WebSettings
webSettings=webView.getSettings();
webSettings.setBuiltInZoomControls(true);
webView.loadUrl("http://www.yahoo.com.tw");
return rootView;
}
private class CallBack extends
WebViewClient{
@Override
public boolean
shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
}
}
}
WebView取得外部網站於app內觀看 / Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.robert.sample12"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19"
/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name="com.robert.sample12.MainActivity"
android:label="@string/app_name"
>
</activity>
<activity
android:name="com.robert.sample12.RadioActivity"
android:label="@string/title_activity_radio"
>
</activity>
<activity
android:name="com.robert.sample12.CheckBoxActivity"
android:label="@string/title_activity_check_box"
>
</activity>
<activity
android:name="com.robert.sample12.StarActivity"
android:label="@string/title_activity_star"
>
</activity>
<activity
android:name="com.robert.sample12.AutoCompleteActivity"
android:label="@string/title_activity_auto_complete"
>
</activity>
<activity
android:name="com.robert.sample12.WebBrowerActivity"
android:label="@string/title_activity_web_brower"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
沒有留言:
張貼留言