如何将图标放入自定义对话框的标题中

发布于 2024-10-04 12:39:20 字数 424 浏览 3 评论 0原文

我想将一个可绘制对象放入对话框标题栏中。我尝试了以下操作:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

无力看清 2024-10-11 12:39:20

show() 之后调用 setFeatureDrawableResource()

不知道为什么会这样。 :)

Call setFeatureDrawableResource() after show().

No idea why this works. :)

笑脸一如从前 2024-10-11 12:39:20

这是解决方案

final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setTitle(R.string.my_dialog_title);
dialog.setContentView(R.layout.my_dialog_layout);
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.some_icon);
dialog.show(); 

如果您希望对话框看起来像一个活动,则将主题添加到对话框中,如下所示

final Dialog dialog = new Dialog(this,AlertDialog.THEME_HOLO_LIGHT);  

Here is solution

final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setTitle(R.string.my_dialog_title);
dialog.setContentView(R.layout.my_dialog_layout);
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.some_icon);
dialog.show(); 

If you want your dialog look like a activity than add theme to dialog as follow

final Dialog dialog = new Dialog(this,AlertDialog.THEME_HOLO_LIGHT);  
星軌x 2024-10-11 12:39:20

感谢 Smaïl Hammour 的帖子,我让它以不同的方式工作。

将此静态方法放入您首选的工具类中:

public static void msgBox( String msg, String title, int type, final Context c){

    int theIcon = drawable.ic_dialog_alert;

    switch(type){
    case YourToolClass.CONFIRMATION:
        theIcon = drawable.ic_menu_help;
        break;      
    case YourToolClass.INFO:
        theIcon = drawable.ic_dialog_info;
        break;
    case YourToolClass.ALERT:
    default:
    }

AlertDialog.Builder builder = new AlertDialog.Builder(c);

    /* Here enters the .setIcon: */
builder.setMessage(msg) .setTitle (title) .setIcon(theIcon);

builder.setPositiveButton( "OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        /*  */
    }
}); 


AlertDialog dialog = builder.create(); 
dialog.show();

}

调用:

YourToolClass.msgBox("the main message goes here", "Test", getBaseContext());

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:

public static void msgBox( String msg, String title, int type, final Context c){

    int theIcon = drawable.ic_dialog_alert;

    switch(type){
    case YourToolClass.CONFIRMATION:
        theIcon = drawable.ic_menu_help;
        break;      
    case YourToolClass.INFO:
        theIcon = drawable.ic_dialog_info;
        break;
    case YourToolClass.ALERT:
    default:
    }

AlertDialog.Builder builder = new AlertDialog.Builder(c);

    /* Here enters the .setIcon: */
builder.setMessage(msg) .setTitle (title) .setIcon(theIcon);

builder.setPositiveButton( "OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        /*  */
    }
}); 


AlertDialog dialog = builder.create(); 
dialog.show();

}

To invoke:

YourToolClass.msgBox("the main message goes here", "Test", getBaseContext());
千紇 2024-10-11 12:39:20

您还可以像这样扩展 Dialog 类:

public class CustomDialog extends Dialog {

    public CustomDialog(Context context) {
        super(context);
        setTitle("Some Title");
        requestWindowFeature(Window.FEATURE_LEFT_ICON);
        setContentView(R.layout.my_layout);
    }

    @Override
    protected void onStart() {
        setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.my_icon);
        super.onStart();
    }

即在构造函数中准备窗口功能,然后在 onStart 中设置具体资源。

因此,在您的主代码中您可以简单地使用:

    CustomDialog cd = new CustomDialog(getActivity());
    rd.show();

You can also extend the Dialog class like so:

public class CustomDialog extends Dialog {

    public CustomDialog(Context context) {
        super(context);
        setTitle("Some Title");
        requestWindowFeature(Window.FEATURE_LEFT_ICON);
        setContentView(R.layout.my_layout);
    }

    @Override
    protected void onStart() {
        setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.my_icon);
        super.onStart();
    }

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:

    CustomDialog cd = new CustomDialog(getActivity());
    rd.show();
无人问我粥可暖 2024-10-11 12:39:20

这是解决方案。按照食谱操作,您将拥有您的图标!
注意:顺序非常重要。

final Dialog yourDialog = new Dialog(YourClass.this);
yourDialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);  //must come BEFORE setContentView
yourDialog.setContentView(R.layout.yourDialog_layout);
yourDialog.setTitle("Your Title");
yourDialog.setCancelable(true);  yourDialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.your_icon);  //must come AFTER setContentView

Here is the solution. Follow the recipe and you shall have your icon !
Note: order is very important.

final Dialog yourDialog = new Dialog(YourClass.this);
yourDialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);  //must come BEFORE setContentView
yourDialog.setContentView(R.layout.yourDialog_layout);
yourDialog.setTitle("Your Title");
yourDialog.setCancelable(true);  yourDialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.your_icon);  //must come AFTER setContentView
世态炎凉 2024-10-11 12:39:20
setIcon(R.drawable.image_name)
setIcon(R.drawable.image_name)
在梵高的星空下 2024-10-11 12:39:20

像这样调用 setFeatureDrawableResource

 dialog.show();
 dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.logo_1x);

,即在调用dialog.show()之后在我的情况下完美地工作..谢谢..:)

calling setFeatureDrawableResource llike this

 dialog.show();
 dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.logo_1x);

i.e after calling dialog.show() worked perfectly in my case .. thanks .. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文