【问题标题】:Dialogue Like Activity is not Finishing on touch outside类似活动的对话未在外部触摸完成
【发布时间】:2025-12-25 13:20:20
【问题描述】:

我创建了一个看起来像对话框的活动。这是我为实现的目标所做的

requestWindowFeature(Window.FEATURE_NO_TITLE);

Display display = getWindow().getWindowManager().getDefaultDisplay();
LayoutParams params = getWindow().getAttributes(); 
params.height = (display.getHeight()*3)/4;
params.width = (display.getWidth()) / 2;
params.alpha = 1.0f;
params.dimAmount = 0.5f;
params.gravity=Gravity.TOP | Gravity.RIGHT;;

getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); 
setContentView(R.layout.search_activity);

 LinearLayout layout = (LinearLayout)findViewById(R.id.root);
 LinearLayout.LayoutParams lp = new      
 LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,       
 LinearLayout.LayoutParams.MATCH_PARENT);
 lp.setMargins(0, getIntent().getExtras().getInt("height"), 0, 0);
 layout.setLayoutParams(lp);

我用来创建这个的样式

  <style name="PopupTheme" parent="android:Theme.Holo.Light.Dialog">
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowSoftInputMode">stateAlwaysHidden</item>
    <item name="android:windowActionModeOverlay">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowCloseOnTouchOutside">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    </style>

这是我得到的: 蓝色区域是一项活动。实际上,当我触及活动的一侧时,我的活动必须从视野中消失。它正在工作。但是活动还没有完成。我想在我触摸它时完成它

我试过this.setFinishOnTouchOutside(true);

@Override
  public boolean onTouchEvent(MotionEvent event) {
    // If we've received a touch notification that the user has touched
    // outside the app, finish the activity.
    if (MotionEvent.ACTION_OUTSIDE == event.getAction()) {
      finish();
      return true;
    }

    // Delegate everything else to Activity.
    return super.onTouchEvent(event);
  }

我知道这一行&lt;item name="android:windowCloseOnTouchOutside"&gt;true&lt;/item&gt; 有助于从视图中关闭活动。但是有没有办法以编程方式访问此属性。 但没有帮助我。谁能告诉我如何实现它??

【问题讨论】:

    标签: android android-activity dialog themes


    【解决方案1】:

    试试这个

    public void onPause(){
    super.onPause();
    finish();
    }
    

    【讨论】:

      最近更新 更多