通过扩展 Dialog 或 AlertDialog 自定义对话框
我想制作一个自定义Dialog
。因为我不喜欢它的风格,所以我想要圆角矩形而不是尖角。我知道如何在 AndroidManifest.xml
中按主题实现它,例如,我使用:
android:theme="@style/Theme.CustomDialog"
和 Theme.CustomDialog.xml
:
<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">@drawable/filled_box</item>
<item name="android:windowNoTitle">true</item>
filled_box.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffffff"/>
<stroke android:width="3dp" color="#ffff8080"/>
<corners android:radius="30dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
如何通过扩展来实现类似的结果对话框
还是AlertDialog
?
I want to make a custom Dialog
. Because I don't like its style, I want to have rounded rectangle rather than sharp corners. I know how to implement it by theme in AndroidManifest.xml
, for example, I use:
android:theme="@style/Theme.CustomDialog"
And Theme.CustomDialog.xml
:
<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">@drawable/filled_box</item>
<item name="android:windowNoTitle">true</item>
filled_box.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffffff"/>
<stroke android:width="3dp" color="#ffff8080"/>
<corners android:radius="30dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
How can I implement a similar result by extending the Dialog
or AlertDialog
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在扩展 Dialog 的类的构造函数中,调用
super(context, R.style.CustomDialog);
我已经多次这样做来创建具有特定主题的自定义对话框。但是,如果主题是您想要更改的对话框的唯一内容,您可以尝试仅实例化 Dialog 类的实例并向其传递主题 ID,如
Dialogdialog = new Dialog(context, R.style. CustomDialog);
扩展 Dialog 的示例:
您将添加到此类的其余代码将与您在 Activity 类中编写的代码非常相似。
In the constructor of your class that extends Dialog call
super(context, R.style.CustomDialog);
I've done this many times to create custom dialogs with specific themes.However if the theme is the only thing about the Dialog that you want to change, you could try just instantiating an instance of the Dialog class and pass it the theme ID like
Dialog dialog = new Dialog(context, R.style.CustomDialog);
An example of extending Dialog:
The rest of the code you will add to this class is going to be pretty much exactly like what you would write in an Activity class.