Ich mache eine einfache Android-App, die eine Webseite in einer Webansicht lädt und es gibt einen Begrüßungsbildschirm als Ladebildschirm in einem Bildfenster, das zu Beginn mit URL-Laden erscheint. Mein Problem ist, dass ich hier verschiedene Bilder entsprechend der Bildschirmausrichtung zu ladende mein Code activity_main.xmlChange Imageview Image Src auf Bildschirmausrichtung ändern
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.example.MainActivity">
<ImageView android:id="@+id/imageLoading1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:visibility="visible"
android:src="@mipmap/vert_loading" />
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
</RelativeLayout>
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends Activity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("https://www.example.com/");
mWebView.setWebViewClient(new MyAppWebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
}
});
}
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
@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();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
}
Ich habe ein anderes Bild für die Landschaft Layout im selben Ordner, aber wie man es lädt?
Warum haben Sie meinen Code kopiert? – Grender
warum nicht? Es löst das Problem, aber mein Hinweis war nicht die OnConfigurationChanged-Methode, sondern onCreate verwenden. Ich weiß nicht, was dein Problem ist, ich habe deinen Namen gepostet. – ChampS
Aber Ihre Antwort ist nicht korrekt, wenn Sie die Orientierung außerhalb 'onConfigurationChanged()' Methode wissen möchten, müssen Sie 'yourActivity.getResources(). GetConfiguration(). Orientation'. Sie sollten den Code vor dem Kopieren von jedem überprüfen. – Grender