Ich wollte eine einfache Android-App erstellen, die nur ein WebView enthält, um meine Website anzuzeigen. Ich habe die offical android tutoiral dazu verwendet.Warum kann ich meine Website in einer App sehen, aber nicht in einer anderen?
Für meine erste App habe ich die Fullscreen-Vorlage verwendet. Nach der Zugabe sieht die WebView meine xml
-Dateien wie folgt aus:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context="com.example.janwagner.webviewdemo.FullscreenActivity">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<TextView
android:id="@+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:text="@string/dummy_content"
android:textColor="#33b5e5"
android:textSize="50sp"
android:textStyle="bold" />
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/fullscreen_content_controls"
style="?metaButtonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="@color/black_overlay"
android:orientation="horizontal"
tools:ignore="UselessParent">
<Button
android:id="@+id/dummy_button"
style="?metaButtonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/dummy_button" />
</LinearLayout>
</FrameLayout>
</FrameLayout>
Und fügte ich diesen Code java
-Dateien hte:
WebView myWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://192.168.100.166:8080");
Und ich hinzugefügt, um die Internet-Erlaubnis zu dem Manifest als direkte Kind die <manifest>
-Block:
<uses-permission android:name="android.permission.INTERNET" />
Dies funktioniert gut. Da ich aber mit der resuling App nicht ganz zufrieden war, habe ich ein neues Projekt basierend auf der 'leeren Aktivität'-Vorlage erstellt. Dies führte zu diesem viel kürzer xml
-datei:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.janwagner.trainerapp.MainActivity">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</android.support.constraint.ConstraintLayout>
Und ich den gleichen Code in der java
-Dateien eingefügt Kopie:
WebView myWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl(this._url);
Und fügte ich wieder das Internet Erlaubnis:
<uses-permission android:name="android.permission.INTERNET" />
Diese App funktioniert nicht. Jedoch Daten mit
myWebView.loadData("<h1>Test</h1>", "text/html; charset=UTF-8", null);
zeigt -Test geladen.
Kann mir jemand sagen, warum die erste Version funktioniert, aber nicht die zweite?
Sie können Einschränkungen hinzufügen müssen. Das Constraint-Layout muss mit Parametern wie app: layout_constraintLeft_toLeftOf = "parent" app: layout_constraintRight_toRightOf = "übergeordnete App: layout_constraintTop_toTopOf =" parent "usw. versehen sein. Sehen Sie sich diese https://developer.android.com/reference/android/ an. support/constraint/ConstraintLayout.html – Stallion