轉自:http://blog.csdn.net/u011007829/article/details/47293597
在最近的項目中需要用到一個類似于toast效果的警告框,而且還要是圓角的。下面是我實作的效果截圖:
首先定義一個dialog:
[java] view
plaincopy
package com.bobge.doura.customview;
import android.app.dialog;
import android.content.context;
import android.view.layoutinflater;
import android.view.view;
import android.widget.linearlayout;
import android.widget.textview;
import com.bobge.doura.r;
/**
* created by administrator on 2015/6/23.
*/
public class customdialog extends dialog {
public customdialog(context context, int theme) {
super(context, theme);
}
public customdialog(context context) {
super(context);
/**
* helper class for creating a custom dialog
*/
public static class builder {
private context context;
private string message;
private view contentview;
public builder(context context) {
this.context = context;
}
/**
* set the dialog message from string
*
* @return
*/
public builder setmessage(string message) {
this.message = message;
return this;
* set the dialog message from resource
public builder setmessage(int message) {
this.message = (string) context.gettext(message);
* set a custom content view for the dialog.
* if a message is set, the contentview is not
* added to the dialog...
* @param v
public builder setcontentview(view v) {
this.contentview = v;
* create the custom dialog
public customdialog create() {
layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service);
// instantiate the dialog with the custom theme
final customdialog dialog = new customdialog(context, r.style.dialog);
view layout = inflater.inflate(r.layout.warm_dialog, null);
dialog.addcontentview(layout, new linearlayout.layoutparams(linearlayout.layoutparams.wrap_content, linearlayout.layoutparams.wrap_content));
// set the content message
if (message != null) {
((textview) layout.findviewbyid(r.id.tv_warmdialog)).settext(message);
}
dialog.setcontentview(layout);
return dialog;
}
寫了一個工具類來顯示該dialog:
package com.bobge.doura.helpr;
import android.os.handler;
import com.bobge.doura.customview.customdialog;
* created by administrator on 2015/6/18.
public class toastshow {
public static void showcustomdialog(string warminfo, context context) {
customdialog.builder custombuilder = new
customdialog.builder(context);
custombuilder.setmessage(warminfo);
final customdialog customdialog = custombuilder.create();
customdialog.show();
handler handler = new handler();
handler.postdelayed(new runnable() {
public void run() {
customdialog.dismiss();
}, 1000);
最重要的一點是要給dialog設定一個style:
[html] view
<style name="dialog" parent="@android:style/theme.dialog">
<item name="android:windowframe">@null</item>
<item name="android:windowisfloating">true</item>
<item name="android:windowistranslucent">true</item>
<item name="android:windownotitle">true</item>
<item name="android:background">@drawable/shape_dialog</item>
<item name="android:windowbackground">@drawable/shape_dialog</item>
<item name="android:backgrounddimenabled">true</item>
<item name="android:backgrounddimamount">0</item>
</style>
<item name="android:backgrounddimamount">0</item>
這一句是設定背景的灰階,這裡設定的是0,表示背景灰階完全透明!如此可以實作上面說的效果。