android系统的默认对话框是黑色背景,白色边框的样式,对于android系统来说是相当漂亮的,可是与自己的项目风格不搭,所以只好想办法重写他的样式了,当然dialog是支持样式重写的
使用new Dialog(context, style).setContentView(layout);
即可定制属于自己项目的dialog
当然,这儿的style起到了至关重要的作用,要知道那个与项目不搭的白边得全靠它了
在value下新建style.xml
下面分享一下我的代码:
首先:MainActivity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
protected
void
onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public
void
dialogStyle(View v){
final
Dialog d=
new
Dialog(
this
, R.style.dialog);
View vv = LayoutInflater.from(
this
).inflate(R.layout.dialog_test,
null
);
TextView delete = (TextView) vv.findViewById(R.id.delete);
d.setCanceledOnTouchOutside(
true
);
d.setContentView(vv);
d.show();
delete.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View arg0) {
// TODO Auto-generated method stub
d.dismiss();
}
});
}
}
|
然后:style
1
2
3
4
5
6
7
8
9
|
<style name=
"dialog"
parent=
"@android:style/Theme.Dialog"
>
<item name=
"android:windowFrame"
>
@null
</item>
<item name=
"android:windowIsFloating"
>
true
</item>
<item name=
"android:windowIsTranslucent"
>
false
</item>
<item name=
"android:windowNoTitle"
>
true
</item>
<item name=
"android:background"
>
@null
</item>
<item name=
"android:backgroundDimEnabled"
>
false
</item>
<item name=
"android:windowBackground"
>
@android
:color/transparent </item>
</style>
|
最后xml
1
2
3
4
5
6
7
8
9
|
<!--?xml version=
"1.0"
encoding=
"utf-8"
?-->
<linearlayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:orientation=
"vertical"
>
<relativelayout android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
>
<textview android:id=
"@+id/delete"
android:layout_width=
"238dp"
android:layout_height=
"58dp"
android:background=
"#2c2c2c"
android:text=
"删除联系人"
android:textcolor=
"#ffffff"
android:gravity=
"center"
android:layout_centerhorizontal=
"true"
>
</textview></relativelayout>
</linearlayout>
|