我使用的服务是在其中向WindowManager添加视图的。在此视图中,我有一个ImageView(通过xml布局)。 ImageView有一个onClickListener,我可以接收点击事件。我添加了一个选择器,但它不会更改正常/按下状态。
当我将相同的ImageView添加到Activity布局时,它将起作用。我在做什么错了?
我在WindowManager中添加了以下参数:
private val params : WindowManager.LayoutParams = WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY }else{ WindowManager.LayoutParams.TYPE_SYSTEM_ERROR },
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON or
WindowManager.LayoutParams.FLAG_FULLSCREEN,
PixelFormat.TRANSLUCENT)
...
val view = LayoutInflater.from(this).inflate(R.layout.overlay_call, null)
params.windowAnimations = android.R.style.Animation_Toast
windowManager.addView(view, params)
这是我的视图布局:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/backgroundLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/default_padding"
android:background="@color/md_re_grey">
<ImageView
android:id="@+id/button_answer_call"
android:layout_width="80dp"
android:layout_height="80dp"
android:clickable="true"
android:focusable="true"
android:background="@drawable/call_button"
android:contentDescription="@string/answer_call"
android:padding="20dp" />
</LinearLayout></FrameLayout>
选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="false">
<layer-list>
<item>
<shape android:shape="oval">
<solid android:color="@color/call_button_normal"/>
</shape>
</item>
<item
android:drawable="@drawable/ic_call_white_36dp"
android:bottom="20dp"
android:left="20dp"
android:right="20dp"
android:top="20dp"/>
</layer-list>
</item>
<item
android:state_pressed="true">
<layer-list>
<item>
<shape android:shape="oval">
<solid android:color="@color/call_button_pressed"/>
</shape>
</item>
<item
android:drawable="@drawable/ic_call_white_36dp"
android:bottom="20dp"
android:left="20dp"
android:right="20dp"
android:top="20dp"/>
</layer-list>
</item>