2016-05-03 13 views
0

私はwebviewでウェブページを読み込む簡単なアンドロイドアプリを作っています。最初にURLを読み込んで表示される画像ビューに読み込み画面としてスプラッシュ画面があります。 私の問題は、私はここで画面の向き に応じて異なる画像をロードすることです私のコード activity_main.xml画像の向きを画像の向きに変更する

<?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); 
    } 


} 

私は風景のレイアウトのために別の画像を持っています同じフォルダーにあるが、ロードする方法は?

答えて

0

方向ごとに異なるレイアウトフォルダを作成できます。

  • layout-portポートレートモードです。
  • layout-landランドスケープモード。

それとも、あなたはActivity内のみimage srcあなたが向きを検出することができます変更し、イベントを変更し、新しいイメージをロードする場合。

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 

    // Checks the orientation of the screen 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
     // Set landscape image src 
    else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ 
     // Set portrait image src 
} 
0

Grenderに記載されているように、ポートレートとランドスケープの異なるレイアウトを読み込むことができます。 また、プログラム的に使用することもできます。

onCreateメソッドでimageview srcを設定して使用します。あなたの画面の向きを変更した場合

int orientation = Activity.getResources().getConfiguration().orientation 
if (orientation == Configuration.ORIENTATION_LANDSCAPE) 
    // Set landscape image src 
else if (orientation == Configuration.ORIENTATION_PORTRAIT){ 
    // Set portrait image src 

だから、のonCreateメソッドが再び呼び出されると、このコードは、あなたのImageViewのは、使用すべきsrcを決定しました。

編集: onCreateのヒントについては、Grenderのおかげであなたの活動リソースを使用する必要があります。

+0

なぜ私のコードをコピー貼り付けましたか? – Grender

+0

なぜですか?それは問題を解決しますが、私のヒントはonConfigurationChangedメソッドを使用するのではなく、onCreateを使用することでした。あなたの問題が何であるかわからない、私はあなたの名前を掲載しました。 – ChampS

+0

しかし、あなたの答えが正しくない場合、 'onConfigurationChanged()'メソッドの外側でオリエンテーションを知りたい場合は、 'yourActivity.getResources()。getConfiguration()。orientation'を使う必要があります。コピーする前にコードをチェックしてください。 – Grender

関連する問題