如何啓動加載簡單HTML頁面的基本項目。
如果您空活動
首先創建一個新的項目選擇,你需要定義你的佈局。你需要一個網頁視圖佈局添加到您的activity_main.xml中
activity_main.xml中位於下水庫 - >佈局
此網頁視圖佈局:
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/webView"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
父佈局 - 標籤需要是<RelativeLayout
您需要做的第二件事是定義您需要的權限,您可以在AndroidManifes中執行此操作t.xml
AndroidMainfest。XML是根據清單
位於您需要添加此權限:
<uses-permission android:name="android.permission.INTERNET" />
如果您忘記這些許可,它會給你一個錯誤,因爲他無法找到該網頁,即使它是一個本地文件這需要許可。
webview的一個問題是,如果您旋轉手機,您的活動將重新啓動。這不是用戶友好的,所以將視圖鎖定爲肖像。 因此,我們需要做的是這樣的:
<activity android:name=".MainActivity"
android:configChanges="orientation"
android:screenOrientation="portrait">
所以你AndroidManifest會是這個樣子:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="YOUR_PACKAGE_NAME">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:configChanges="orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- ATTENTION: This data URL was auto-generated. We recommend that you use the HTTP scheme.
TODO: Change the host or pathPrefix as necessary. -->
<data
android:host="[ENTER-YOUR-HTTP-HOST-HERE]"
android:pathPrefix="/main"
android:scheme="http" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
現在將Java代碼添加到在MainActivity加載網頁我們想要:
MainActivity.java位於java - > [YOUR_PACKAGE_NAME]
第一行我們將定義我們的類:
public class MainActivity extends Activity
在我們寫一些功能將
在那裏,第一個功能是創建web視圖。
我們聲明瞭一個私有的var Webview。編寫更少的代碼。
private WebView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setAllowFileAccess(true);
view.getSettings().setDomStorageEnabled(true);
view.setWebViewClient(new MyBrowser() {});
view.loadUrl("HERE_YOUR_LINK");
view.setWebChromeClient(new WebChromeClient() {});
}
如果要加載本地文件的鏈接將是這樣的:
view.loadUrl("file:///android_asset/www/index.html");
如果要加載像Google這樣的網站將是:
view.loadUrl("https://www.google.com/");
稍後我會說你需要找到你想要加載的本地HTML文件的位置。
如果您的網站上有類似鏈接mailto:
的內容。它認爲,如果用戶點擊它,它會打開Gmail應用或其他電子郵件應用。您可以通過創建OverrideUrlLoading
方法來完成此操作。
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if (url.startsWith("tel:") || url.startsWith("sms:") || url.startsWith("smsto:") || url.startsWith("mailto:") || url.startsWith("mms:") || url.startsWith("mmsto:") || url.startsWith("market:") || url.startsWith("https://youtu")){
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
} else {
view.loadUrl(url);
return true;
}
}
}
就像你可以說我也加了url.startsWith("https://youtu")
。我個人因爲我覺得用戶友好,你鏈接到一個Youtube視頻,它將在Youtube應用中打開,因爲人們可以全屏查看它。
我們需要的唯一功能是如果用戶點擊手機,該做什麼。
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && view.canGoBack()) {
view.goBack(); //method goback()
return true;
}
return super.onKeyDown(keyCode, event);
}
您需要將這些行添加到您的版本。gradle這個:
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.gms:google-services:3.1.0'
因此,這將是這個樣子:
//here is so more code in the file
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.gms:google-services:3.1.0'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
如果要加載的,而不是一個網站,也可以本地文件。 但我需要放置它們在哪裏?
您需要創建一個名爲assets的地圖。
該地圖位於何處。您的PC上轉到您的文件,探索 瀏覽到這一點:
YOUR_PACKAGE_NAME - >程序 - > SRC - >主
在該文件夾,您將創建一個映射assets
和那裏的地圖www
那裏要粘貼文件
如果您有任何疑問,請回復
確實有Android的一些經驗?在線網站包含Js/Jquery和HTML? – Steven
我在1. {東西}的日子裏回顧了它,但我可能需要從頭開始。是的,前端是純HTML和JS。 – Robert
您是否願意將本地網站存儲在設備上或使用https鏈接進行操作? – Steven