}
public float getDensity(Context context) {
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
return dm.density;
}
public CustomDialog(Context context, int width, int height, int layout, int style) {
super(context, style);
// 设置内容
setContentView(layout);
// 设置窗口属性
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
// 设置宽度、高度、密度、对齐方式
float density = getDensity(context);
params.width = (int) (width * density);
params.height = (int) (height * density);
params.gravity = Gravity.CENTER;
window.setAttributes(params);
dialogText = (TextView) findViewById(R.id.dialogText);
new Thread(new TimeThread()).start();
}
final Handler timeHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
if (progress < 100) {
progress = progress + new Random().nextInt(10);
if (progress <= 100) {
dialogText.setText(“更新进度” + progress + “%”);
} else {
dialogText.setText(“更新进度100%”);
}
} else {
dialogText.setText(“更新完成”);
dismiss();
}
}
super.handleMessage(msg);
}
};
public class TimeThread implements Runnable {
@Override
public void run() {
while
(true) {
try {
Thread.sleep(1000);
Message message = new Message();
message.what = 1;
timeHandler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@Override
public void dismiss() {
super.dismiss();
}
}
自定义Toast和Notification
文章末尾有代码链接,首先贴个效果图,下拉通知里面的是Notification,居中的是自定义的Toast:
主函数的布局文件只有两个按钮,分别是Toast和N