0

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

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);
  }

我知道这条线<item name="android:windowCloseOnTouchOutside">true</item>有助于从视图中关闭活动。但是有没有办法以编程方式访问这个属性。但没有帮助我。谁能告诉我我怎样才能实现它?

4

1 回答 1

0

尝试这个

public void onPause(){
super.onPause();
finish();
}
于 2013-07-25T20:23:35.583 回答