使用Layout Inflater后,ImageView中的布局图像源未更改 [英] Layout image source Is not changing in ImageView after using Layout Inflater

查看:90
本文介绍了使用Layout Inflater后,ImageView中的布局图像源未更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to change the image in an ImageView of a different layout. For this, I am using Layout Inflator, because I don't know any other way. The image gets visible when users click a button and according to the selected button, image gets visible. But this method is not working for me.

这是我的布局文件:

<?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>

这是我正在使用的代码:

And this is the code which I am using:

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

这是我的服务班级:

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

我的应用程序正在服务上使用,因此我在服务类和主要活动中使用了此代码,但没有用.另外,我尝试在启动时运行它,而不是在按下按钮时使用它,但仍然对我不起作用.

My app is working on a service so I used this code in the service class and also on the main activity but it didn't work. Also, I tried to run it on start instead of using it on button press but still didn't work for me.

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

I am also not sure is this the right approach to do this any suggestion is also great. I am new in programming. Also, suggest that the source image I am changing still stays the same because the app might get closed but by using service app will still show the image, so what is the best option for that.

推荐答案

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

I don't understand your question, but I can see some big flaws in posted code...

首先,这些代码不属于Service

First of all this code do not belong to 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来显示用户

in second line you are inflating new View v without adding to parent (second param is null), so this View isn't shown to user anywhere... besides of that Service don't have own layout so you don't have any place to add this View for showing user

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

I think you are misunderstanding what inflater is... XML resources are like "sketches", inflater is creating new View basing on XML code, so every result is new object. If you change image on one View it won't automatically change in another... In other words, by code:

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

iv2 will be still showing R.drawable.my_image, because it belongs to new, freshly created (inflated) View v2

我只是怀疑您是在Activity内部对这个View进行膨胀,并认为当您在代码中的任何其他位置对其进行膨胀并更改其外观(图像)时,所有膨胀的View都会改变有了这个id ...就不会了

I'm just suspecting that you are inflating this View inside Activity and think that when you inflate it in any other place in code and change its look (image) it will change in all inflated Views with this id... it won`t

这篇关于使用Layout Inflater后,ImageView中的布局图像源未更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆