将activity作为dialog使用的方法:

##将activity作为dialog使用的方法:

1、设置activity的属

①在activity的oncreate中设置windowManager
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 | Gravity.BOTTOM;
		layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
		layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
		//设置x与y的偏移量
		layoutParams.x = 200;
		layoutParams.y = 340;
		window.setAttributes(layoutParams);
②复写onTouch方法
@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (MotionEvent.ACTION_OUTSIDE == event.getAction()) {
			finish();
			return true;
		}
		return super.onTouchEvent(event);
	}
③在Androidmanifest中设置style
   <activity
            android:name=".activity.SettingActivity"
            android:launchMode="singleTop"
            android:theme="@style/dialogActivityStyle" />
④定义style的方法
 <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>

2、将activity中的view作为dialog的子view

①在activity中设置子view
private View subview = null;
subview = getLayoutInflater().inflate(R.layout.subview_select_uvccamera, null);
 Dialog dialog = new Dialog(mContext);
 dialog.setSubView(subview);
②在dialog中设置view

//subview为dialog中的一个控件,用于承载view
private LinearLayout subview;
subview = (LinearLayout) mView.findViewById(R.id.subview);

public void setSubView(View view)
	{
		if (view != null)
		{
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
			subview.addView(view, lp);
			getTextViewHeight(subview);
		}
	}

你可能感兴趣的:(安卓基础学习,android)