使用 Layout Inflater 后,布局图像源在 ImageView 中没有改变

Layout image source Is not changing in ImageView after using Layout Inflater

我正在尝试更改不同布局的 ImageView 中的图像。为此,我正在使用 Layout Inflator,因为我不知道任何其他方式。 当用户单击按钮并根据所选按钮时,图像会变得可见。但是这个方法对我不起作用。

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:layout_alignParentStart ="true"
        android:layout_centerVertical = "true"
        android:src="@drawable/my_image"/>

</RelativeLayout>

这是我正在使用的代码:

LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.mylayout, null);
        ImageView iv = (ImageView)v.findViewById(R.id.imageView);
        iv.setImageResource(R.drawable.myImage2);

这是我的服务class:

 @Override
    public void onCreate() {
        super.onCreate();
        SharedPreferences sharedPreferences = this.getSharedPreferences("ON", Context.MODE_PRIVATE);
        n_hi = sharedPreferences.getInt("N_HI",0);
        n_wi = sharedPreferences.getInt("N_WI",0);

        LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.newLayout, null);
        ImageView iv = (ImageView)v.findViewById(R.id.imageView);
        iv.setImageResource(R.drawable.icon);

        changeDesign();
    }

 
 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver,new IntentFilter("SizeChange"));
            if (isPortrait) {
                drawPortrait();
            } else {
                drawLandscape();
            }
        return Service.START_STICKY;
    }
 
 public void drawPortrait(){

        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
            if (d) {
                overlay = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
            }else{
                overlay = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;
            }
        } else {
            overlay = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        }
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
            flag_p = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                    WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
                    WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR |
                    WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT |
                    WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES |
                    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
        } else {
            flag_p = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                    WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
                    WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR |
                    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
        }
        floatingView = View.inflate(getApplicationContext(),R.layout.newLayout, null);
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                n_wi,
                n_hi,
                overlay,
                flag_p,
                PixelFormat.TRANSLUCENT
        );
        params.gravity = Gravity.TOP | Gravity.CENTER;
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        windowManager.addView(floatingView, params);
    }

我的应用程序正在处理一项服务,因此我在服务 class 和主要 activity 中使用了这段代码,但它没有用。另外,我尝试在启动时 运行 它而不是在按下按钮时使用它,但仍然对我不起作用。

我也不确定这样做是否正确,任何建议也都很好。我是编程新手。另外,建议我正在更改的源图像仍然保持不变,因为应用程序可能会关闭,但通过使用服务应用程序仍会显示图像,那么最好的选择是什么。

我不明白你的问题,但我可以在发布的代码中看到一些大缺陷...

首先这个代码不属于Service

LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.mylayout, null);
    ImageView iv = (ImageView)v.findViewById(R.id.imageView);
    iv.setImageResource(R.drawable.myImage2);

在第二行中,你正在膨胀新的 View v 而没有添加到父级(第二个参数是 null),所以这个 View 不会在任何地方显示给用户......此外其中 Service 没有自己的布局,因此您没有任何地方可以添加此 View 来显示用户

我认为您误解了 inflater 是什么...XML 资源就像 "sketches",inflater 正在创建新的 View 基于 XML 代码,所以每个结果都是 new 对象。如果您在一个 View 上更改图像,它不会自动在另一个上更改...换句话说,通过代码:

LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View v1 = inflater.inflate(R.layout.mylayout, null);
    ImageView iv1 = (ImageView) v1.findViewById(R.id.imageView);
    iv1.setImageResource(R.drawable.myImage2);

    View v2 = inflater.inflate(R.layout.mylayout, null);
    ImageView iv2 = (ImageView) v2.findViewById(R.id.imageView);

iv2还会显示R.drawable.my_image,因为它属于新的,刚创建的(膨胀的)View v2

我只是怀疑你在 Activity 内膨胀这个 View 并且认为当你在代码的任何其他地方膨胀它并改变它的外观(图像)时它会改变所有膨胀 Views 与此 id... 它不会`t