启动对话活动但不可见?

时间:2014-11-26 06:55:37

标签: android

我发现一个问题,当我在onCreate中添加一个floatwindow时,无法正常启动对话框!

public void createFloatButton(int status) {

    int screenWidth = mWindowAdmin.getDisplayWidth();
    int screenHeight = mWindowAdmin.getDisplayHeight();
    if (mFloatButton == null) {
        mFloatButton = new FloatButton(mContext);
        if (mLayoutParams == null) {
            mLayoutParams = new LayoutParams();
            mLayoutParams.type = LayoutParams.TYPE_PHONE;
            mLayoutParams.format = PixelFormat.RGBA_8888;
            mLayoutParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE;
            mLayoutParams.gravity = GravityCompat.START | Gravity.TOP;
            mLayoutParams.width = mFloatButton.getViewWidth();
            mLayoutParams.height = mFloatButton.getViewHeight();
            mLayoutParams.x = screenWidth;
            mLayoutParams.y = screenHeight / 2;
        }
        mFloatButton.setLayoutParams(mLayoutParams);
        mWindowManager.addView(mFloatButton, mLayoutParams);  //problem line

        if (status == -1) {
            if (!SwitchUtils.isMobileDataEnabled(mContext) && !WifiAdmin.getInstance(mContext).isWifiEnabled()) {
                mFloatButton.setStatus(FloatButton.STATUS_NONE);
            } else {
                mFloatButton.setStatus(FloatButton.STATUS_NORMAL);
            }
        } else {
            mFloatButton.setStatus(status);
        }
    }
}
onCreate()中调用up代码然后单击按钮以显示对话框,该对话框将不可见!

1 个答案:

答案 0 :(得分:0)

您需要将布局参数更改为:

 if (mLayoutParams == null) {
                mLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                        WindowManager.LayoutParams.WRAP_CONTENT,
                        screenWidth,
                        screenHeight / 2,
                        LayoutParams.TYPE_PHONE,
          LayoutParams.FLAG_NOT_FOCUSABLE|LayoutParams.FLAG_NOT_TOUCH_MODAL,
          PixelFormat.RGBA_8888);
                mLayoutParams.gravity = Gravity.TOP | GravityCompat.START;
            }
            mFloatButton.setLayoutParams(mLayoutParams);
            mWindowManager.addView(mFloatButton, mLayoutParams); 

请注意,我使用wrap-content作为宽度和高度,因为我没有FloatButton类。另外,如果我使用mFloatButton.getWidth()和mFloatButton.getHeight()作为宽度和高度 - 按钮视图将不显示,因为宽度和高度可能为零。

以下是基于您的代码使用Button而不是FloatButton类的工作代码 - 没有状态:

public void createFloatButton() {
    WindowManager.LayoutParams mLayoutParams = null;
    Button mFloatButton = null;

     DisplayMetrics dm = new DisplayMetrics();
     WindowManager mWindowManager = (WindowManager)this.getSystemService(Context.WINDOW_SERVICE);
     mWindowManager.getDefaultDisplay().getMetrics(dm);

    int screenWidth = dm.widthPixels;  
    int screenHeight =  dm.heightPixels;
    if (mFloatButton == null) {
        mFloatButton = new Button(this);
        mFloatButton.setBackgroundResource(R.drawable.ic_launcher);
        // Note: I added background to be able to view button

        if (mLayoutParams == null) {
            mLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    screenWidth,
                    screenHeight / 2,
                    LayoutParams.TYPE_PHONE,
      LayoutParams.FLAG_NOT_FOCUSABLE|LayoutParams.FLAG_NOT_TOUCH_MODAL,
      PixelFormat.RGBA_8888);
            mLayoutParams.gravity = Gravity.TOP | GravityCompat.START;
        }
        mFloatButton.setLayoutParams(mLayoutParams);
        mWindowManager.addView(mFloatButton, mLayoutParams);  // no problem


    }
}

确保添加

 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

到AndroidManifest