【问题标题】:How can I customize the header layout of a dialog如何自定义对话框的标题布局
【发布时间】:2010-12-28 11:03:39
【问题描述】:

在Android中,是否可以自定义对话框的标题布局(图标+文本)布局?或者我可以设置标题文本的自定义字符串值吗?

谢谢。

【问题讨论】:

    标签: android android-layout android-alertdialog android-dialog


    【解决方案1】:

    如果您为对话框和标题设置了自定义布局,则可以更改对话框的标题。我只使用过这种方法来完全删除标题,但这应该适用于自定义标题:

    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);
    

    这有点复杂(因为您还必须设置对话框的布局),但它比子类化 Dialog 更容易。

    【讨论】:

      【解决方案2】:

      原来的 Dialog 类似乎缺乏设置图标的能力,但您可以轻松扩展 AlertDialog 并设置自定义视图(与您的 Dialog 实例使用相同),您只需要这样的东西

       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);
           }
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多