将activity设置成类似为dialog实现方式的操作

将activity设置成类似为dialog实现方式的操作:

1、自定义activity的大小

Window window = getWindow();
		WindowManager.LayoutParams layoutParams = window.getAttributes();
		window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		layoutParams.gravity = Gravity.RIGHT;
		layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
		layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;

		window.setAttributes(layoutParams);

2、修改activity的theme

<style name="dialogActivityStyle" parent="Theme.AppCompat.Dialog">
        <!--设置dialog的背景-->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!--设置Dialog的windowFrame框为无-->
        <item name="android:windowFrame">@null</item>
        <!--设置无标题-->
        <item name="android:windowNoTitle">true</item>
        <!--是否浮现在activity之上-->
        <item name="android:windowIsFloating">true</item>
        <!--是否半透明-->
        <item name="android:windowIsTranslucent">true</item>
        <!--设置窗口内容不覆盖-->
        <item name="android:windowContentOverlay">@null</item>
        <!--设置动画,在这里使用让它继承系统的Animation.Dialog-->
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <!--背景是否模糊显示-->
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowCloseOnTouchOutside">false</item>
    </style>

3、点击activity外部执行自定义的操作

// Make us non-modal, so that others can receive touch events.
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
    // ...but notify us that it happened.
    getWindow().setFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);

完整代码:

@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_main);
		Window window = getWindow();
		window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
		window.setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);

		WindowManager.LayoutParams layoutParams = window.getAttributes();
		window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		layoutParams.gravity = Gravity.RIGHT;
		layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
		layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;

		window.setAttributes(layoutParams);
		}
@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (MotionEvent.ACTION_OUTSIDE == event.getAction()) {
			finish();
			return true;
		}
		return super.onTouchEvent(event);
	}

如果想要自定义设置dialog的位置,通过

layoutParams.width = getResources().getDisplayMetrics().widthPixels - 60;
		layoutParams.height = getResources().getDisplayMetrics().heightPixels - 60;

但是上述方法有个问题,只能设置距离上方,距离左方的距离

如果设置了layoutParams.gravity = Gravity.RIGHT;,可以通过layout_marginEnd进行设置,可实现距离右边的距离。

你可能感兴趣的:(android,java,开发语言)