服务中的膨胀视图显示的是视图,而不是布局内的 ImageView [英] Inflated View in Service is displaying the View, but not an ImageView inside the layout
问题描述
我已经成功地在服务中扩展了我的布局,但是它没有在 Inflated Layout
中显示 ImageView
并且我不知道为什么.我在 imageview 上设置了一个 onClickListener
并且它工作得很好(当我点击绿色方块时记录并停止服务).唯一的问题是它没有显示 X 标记
,我不知道为什么.
I have successfully inflated my layout in a service, however it's not displaying the ImageView
inside the Inflated Layout
and I have no idea why. I set an onClickListener
on the imageview and it works perfectly fine (Logs and stops the service when I click on the green square). The only problem is that it's not displaying the X mark
and I can't figure out why.
public class OverlayService extends Service {
private ViewGroup view;
@Override
public IBinder onBind(Intent i) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.END;
}
view = (ViewGroup) layoutInflater.inflate(R.layout.test_view, null);
ImageView btnClear = view.findViewById(R.id.btn_clear);
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// THIS IS SUCCESSFULLY CALLED WHEN CLICKING ON THE GREEN BUTTON.
Log.d(TAG, "onClick: Clicked");
stopSelf();
}
});
wm.addView(view, params);
}
推荐答案
我很幸运遇到这个答案:
I'm quite lucky running into this answer:
https://stackoverflow.com/a/56045405/11110509
我正要放弃,直到它告诉我尝试
I was about to give up until it told me to try
ImageView
.我不知道为什么会这样,但我现在可以在 Inflated Layout 中看到 drawable.
<androidx.appcompat.widget.AppCompatImageView
in the XML instead of ImageView
. I have no idea why this works, but I can now see the drawable in the Inflated Layout.
这篇关于服务中的膨胀视图显示的是视图,而不是布局内的 ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!