布局图像源在使用布局膨胀后在图像视图中没有更改



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

这是我的布局文件:

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

这是我的服务类:

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

我的应用程序正在处理服务,所以我在服务类和主活动中使用了此代码,但它不起作用。此外,我尝试在启动时运行它,而不是在按下按钮时使用它,但仍然对我不起作用。

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

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

首先,此代码不属于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资源就像"草图",infflat是基于XML代码创建新View,所以每个结果都是对象。如果您在一个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,并认为当您在代码中的任何其他地方对其进行膨胀并更改其外观(图像(时,它将随着此id在所有膨胀View中发生变化......它不会

最新更新