如何将图标放入自定义对话框的标题中
我想将一个可绘制对象放入对话框标题栏中。我尝试了以下操作:
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.some_icon);
dialog.setTitle(R.string.my_dialog_title);
dialog.setContentView(R.layout.my_dialog_layout);
...
图标没有显示,但标题稍微向右移动。对话框似乎为可绘制对象保留了空间,但没有绘制它。我尝试了几个不同的图标(也来自 android 资源),但没有一个起作用。
I'd like to place a drawable into a dialogs title bar. I tried the following:
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.some_icon);
dialog.setTitle(R.string.my_dialog_title);
dialog.setContentView(R.layout.my_dialog_layout);
...
The icon doesn't show up but the title moves a little to the right. It seems the dialog reserves space for the drawable but doesn't draw it. I tried several different icons (also from the android resources) but non of them worked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在
show()
之后调用setFeatureDrawableResource()
。不知道为什么会这样。 :)
Call
setFeatureDrawableResource()
aftershow()
.No idea why this works. :)
这是解决方案
如果您希望对话框看起来像一个活动,则将主题添加到对话框中,如下所示
Here is solution
If you want your dialog look like a activity than add theme to dialog as follow
感谢 Smaïl Hammour 的帖子,我让它以不同的方式工作。
将此静态方法放入您首选的工具类中:
调用:
I got it to work in a different way, thanks to the Smaïl Hammour post.
Place this static method in your preferred tool class:
To invoke:
您还可以像这样扩展
Dialog
类:即在构造函数中准备窗口功能,然后在 onStart 中设置具体资源。
因此,在您的主代码中您可以简单地使用:
You can also extend the
Dialog
class like so:i.e. you prepare your window feature in constructor and then set concrete resource in onStart.
So, in you main code you can simply use:
这是解决方案。按照食谱操作,您将拥有您的图标!
注意:顺序非常重要。
Here is the solution. Follow the recipe and you shall have your icon !
Note: order is very important.
像这样调用 setFeatureDrawableResource
,即在调用dialog.show()之后在我的情况下完美地工作..谢谢..:)
calling setFeatureDrawableResource llike this
i.e after calling dialog.show() worked perfectly in my case .. thanks .. :)