android系统的默认对话框是黑色背景,白色边框的样式,对于android系统来说是相当漂亮的,可是与自己的项目风格不搭,所以只好想办法重写他的样式了,当然dialog是支持样式重写的
使用new Dialog(context, style).setContentView(layout);
即可定制属于自己项目的dialog
当然,这儿的style起到了至关重要的作用,要知道那个与项目不搭的白边得全靠它了
在value下新建style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="dialog" parent="@android:style/Theme.Dialog"><!--name是我们在使用时要用到的资源的标志,parent是指当前的样式所继承的父类样式-->
<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">@color/clarity</item>
<item name="android:windowBackground">@drawable/clarity</item><!--这儿也很重要啊,我这儿用了一张透明的.9.png的图,当然用#00000000也是可以的,否则的话这儿出来后有一个黑色的背景-->
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
下面分享一下我的代码:
首先:MainActivity
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
<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
<?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"
/>
</RelativeLayout>
</LinearLayout>
源码地址:http://download.csdn.net/detail/u014608640/8046705 有需要的朋友可以下载