Android Studio 寫 WebView 的設定 
Monday, December 26, 2016, 11:17 AM - 工作日誌   (同類別文章列表)
Posted by Administrator
有三個檔案要寫:

1. activity_main.xml (APP Layout,APP畫面設定 )
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.a4test.myapplication.MainActivity">

<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="0dp"
android:layout_marginBottom="0dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:id="@+id/webview" />

</RelativeLayout>

主要是WebView裡的部份、拉出一個WebView元件,大部份的東西就會寫好了…
(注意此檔案在下面的頁籤有分Design觀點及Text觀點,差別在於拉放或文字撰寫)


2. MainActivity.java (主要程式檔)
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setDatabaseEnabled(true);

final android.app.Activity activity = this;
myWebView.setWebChromeClient(new android.webkit.WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
}
});

myWebView.loadUrl("http://tkt.nkust.edu.tw/"); }
}

主要是WebView的屬性設定,相當之龜毛與麻煩…


3. AndroidManifest.xml (設定APP權限)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a4test.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

權限設定、但我還搞不清楚…


4.更改 applicationId
需至「Gradle Scripts」裡的「build.gradle(Module:app)」,找到applicationId字串後更改,然後會出現一個燈炮、說明gradle已有變更、要求做變更同步,點選同步,之後就可以了…

發表回應 ( 共計272閱讀人次 )   |  permalink   |  $star_image$star_image$star_image$star_image$star_image ( 2.9 / 331 )

<<第一頁 <前一頁 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 下一頁> 最後>>