问题
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.
This is my layout file:
<?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);
This is my service 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);
}
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.
回答1:
I don't understand your question, but I can see some big flaws in posted code...
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);
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
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
will be still showing R.drawable.my_image
, because it belongs to new, freshly created (inflated) View v2
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 View
s with this id
... it won`t
来源:https://stackoverflow.com/questions/57944143/layout-image-source-is-not-changing-in-imageview-after-using-layout-inflater