Android自定义Dialog
开发时我们可能需要一些非全屏view的一些提示信息,或者不想去创建需要在清单文件注册的Activity来显示view,那么我们就需要借助一些其他的窗体子类来完成需求如:Dialog , PopuWindow,本文介绍Dialog
很简单,无需设置任何style什么的
public class MyDialog extends Dialog {
private View contentView;
private TextView child;
private RelativeLayout pb;
public MyDialog(@NonNull Context context) {
super(context);
}
/**
* 强烈建议在此方法进行自定义view的初始化操作
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去除Dialog本身的背景
initCustomTheme();
createContentView();
setContentView(contentView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
private void createContentView() {
contentView = View.inflate(getContext(), R.layout.mydialog, null);
child = contentView.findViewById(R.id.tv);
pb = contentView.findViewById(R.id.pb);
child.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pb.setVisibility(View.VISIBLE);
}
});
}
private void initCustomTheme() {
Window window = getWindow();
if (window != null){
window.setGravity(Gravity.TOP);//显示在窗口哪个位置,默认中间
window.setDimAmount(0f);//把背景后面的模糊去掉
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//去掉dialog本身的矩形区域去掉(当我们设置自己的圆角框有用)
//window.setWindowAnimations(R.style.DialogAnimation);//动画
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/mydialog"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you sure exit ?" />
<View
android:layout_below="@id/tv"
android:layout_marginTop="15dp"
android:background="#bbffaa"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<LinearLayout
android:layout_below="@id/tv"
android:layout_marginTop="15dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="back"
android:textColor="#eeff00"
android:gravity="center"
android:padding="10dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<View
android:background="#bbffaa"
android:layout_width="1dp"
android:layout_height="match_parent"/>
<TextView
android:text="sure"
android:textColor="#eeff00"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/pb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:visibility="gone">
<ProgressBar
android:indeterminateTint="#fff000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</FrameLayout>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!