背景:
現(xiàn)有的AlertDialog之類的現(xiàn)有對話框類必須得依賴上下文環(huán)境創(chuàng)建,生命周期也受其控制。那么沒有可以依賴的上下文環(huán)境彈框怎么實(shí)現(xiàn),如跨應(yīng)用彈框,暗碼彈框等。
思路:
需要依賴Activity,將Activity直接設(shè)計(jì)成對話框樣式。
步驟:
1.?定義對話框樣式style:

2.在AndroidManifest.xml中設(shè)置activity的theme為自定義style.

3.?創(chuàng)建圓角形狀的shape
<shape xmlns:android="http://schemas.android.com/apk/res/android">
? ? <solid android:color="#ffffff" />
? ? <corners android:radius="12dp" />
</shape>
4.?將shape作為activity窗口的背景。
重點(diǎn):
1.?如何設(shè)置對話框的大小。
方法一:?在oncreate函數(shù)中通過WindowManager和Window設(shè)置:
?????? Window win = this.getWindow();
??????? win.getDecorView().setPadding(0, 0, 0, 0);
??????? WindowManager.LayoutParams lp = win.getAttributes();
? ? ? ? lp.width = 600;
? ? ? ? lp.height = 400;
? ? ? ? lp.gravity = Gravity.CENTER;
? ? ? ? win.setAttributes(lp);
方法二:直接在layout文件中設(shè)置根布局的寬高:
??? android:layout_width="300dp"
? ? android:layout_height="200dp"
2.?如何設(shè)置對話框圓角。
方法一:oncreate中設(shè)置window的背景資源:
win.getDecorView().setBackgroundResource(R.drawable.deviceinfo_dialog_shape);
方法二:style中設(shè)置windowbackground:
<item name="android:windowBackground">@drawable/deviceinfo_dialog_shape</item>。
3.其實(shí)強(qiáng)大的android框架提供了好多屬性可以滿足各種樣式,Dialog的屬性可以參考frameworks/base/core/res/res/values/themes.xml "Theme.Dialog"的屬性集。關(guān)于window的有:
