How can I customize the header layout of a dialog

孤街醉人 提交于 2019-12-23 15:33:15

问题


In Android, is it possible to customize the header layout (the icon + a text) layout of a dialog? Or can I just set a custom string value of the title text?

Thank you.


回答1:


It's possible to change the header of the Dialog if you set a custom layout for both the dialog and the header. I've only ever used this method to remove the header entirely, but this ought to work for a custom header:

dialog = new Dialog(context);
Window window = dialog.getWindow();
window.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
dialog.setContentView(R.layout.my_dialog_layout);
window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_custom_header);

This is all a tad more complicated (as you have to setup the dialog's layout as well) but it's easier than subclassing Dialog.




回答2:


the original Dialog class seems to lack the ability to set an icon, but you can easily extend AlertDialog and set a custom view (the same you would use for your Dialog instance), you just need something like this

 class MyDialog extends AlertDialog {
     public MyDialog(Context ctx) {
        super(ctx);
        LayoutInflater factory = LayoutInflater.from(context);
        View view = factory.inflate(R.layout.dialog_layout, null);
        setView(view);
        setTitle("MyTitle");
        setIcon(R.drawable.myicon);
     }
 }


来源:https://stackoverflow.com/questions/1935257/how-can-i-customize-the-header-layout-of-a-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!