覆盖按钮在Android 4.3中无效

时间:2013-11-11 02:56:15

标签: android android-4.3-jelly-bean android-windowmanager android-overlay android-4.2-jelly-bean

我在WindowManager.LayoutParams.TYPE_TOAST级别图层上有一个带有按钮的叠加视图。该按钮在Android 4.2中运行良好,但是当我在Android 4.3中运行相同的代码时,该按钮无法点击 - 单击它时没有任何反应。

这是相关代码。

ButtonOverlayService.java

@Override
public IBinder onBind(Intent intent) {
    return null;
}

public class ButtonOverlayService extends Service {
    WindowManager wm;
    View home_button_view;

    @Override
    public void onCreate() {
        super.onCreate();

        WindowManager.LayoutParams home_params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                -10000,
                10000,
                WindowManager.LayoutParams.TYPE_TOAST,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT);

        wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

        LayoutInflater layoutInflater = LayoutInflater.from(this);
        home_button_view = layoutInflater.inflate(R.layout.home_button_overlay, null);
        home_button_view.findViewById(R.id.home_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("myApp", "clicked")
            }
        }

        wm.addView(home_button_view, home_params);
    }
}

home_button_overlay.xml

<?xml version="1.0" encoding="utf-8"?>

<Button
    android:id="@+id/home_button"
    android:layout_width="100dp"
    android:layout_height="100dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#85ff3f"
    android:padding="0dp"
    android:paddingEnd="0dp"
    android:paddingStart="0dp"
    android:gravity="fill"></Button>

有人有任何想法吗?

1 个答案:

答案 0 :(得分:2)

使用WindowManager.LayoutParams.TYPE_SYSTEM_ALERT使视图在4.3中可点击。

TYPE_TOAST不再允许点击。