繁体   English   中英

在屏幕方向更改时更改imageview image src

[英]Change imageview image src on screen orientation change

我正在制作一个简单的android应用程序,该程序可在webview中加载网页,并且在以URL加载开始时出现的imageview中有一个初始屏幕作为加载屏幕。 我的问题是我想根据屏幕方向加载不同的图像,这是我的代码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);
    }


}

我在同一文件夹中有用于景观布局的另一张图像,但如何加载呢?

您可以为每个方向创建不同的布局文件夹。

  • 纵向模式的layout-port
  • 用于景观模式的layout-land

或者,如果您只想更改image src ,则可以在Activity检测到方向更改的事件并加载新图像。

@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
}

如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。

编辑:感谢Grender的提示,在onCreate中您应该使用活动资源。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM