You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何自定义Dialog标题并在标题中添加图片?

嘿,这个需求其实挺常见的,我给你两种实用的实现方式,你可以根据自己的场景灵活选择:

方法一:替换系统默认的Dialog标题栏

这种方式适合你已经在使用AlertDialog.Builder构建对话框,只想替换标题部分的场景:

  1. 先创建自定义标题布局
    新建一个XML布局文件(比如dialog_custom_title.xml),把图片和标题文本组合进去:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="16dp"
        android:gravity="center_vertical">
    
        <!-- 标题左侧的图片 -->
        <ImageView
            android:id="@+id/iv_title_icon"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:src="@drawable/your_title_icon"
            android:marginEnd="8dp"/>
    
        <!-- 标题文本 -->
        <TextView
            android:id="@+id/tv_title_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自定义标题"
            android:textSize="18sp"
            android:textStyle="bold"/>
    </LinearLayout>
    
  2. 在代码中设置自定义标题
    构建Dialog时,用setCustomTitle()方法替换默认标题,还能动态修改图片和文本:

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    
    // 加载自定义标题布局
    View customTitleView = LayoutInflater.from(context).inflate(R.layout.dialog_custom_title, null);
    // 动态修改标题图片和文本
    ImageView titleIcon = customTitleView.findViewById(R.id.iv_title_icon);
    titleIcon.setImageResource(R.drawable.your_new_icon);
    TextView titleText = customTitleView.findViewById(R.id.tv_title_text);
    titleText.setText("我的专属标题");
    
    // 设置自定义标题
    builder.setCustomTitle(customTitleView);
    // 设置你原来的对话框内容布局
    builder.setView(LayoutInflater.from(context).inflate(R.layout.your_dialog_content, null));
    
    // 创建并显示Dialog
    AlertDialog dialog = builder.create();
    dialog.show();
    

方法二:完全自定义Dialog的整体布局

如果你需要更彻底的样式控制,直接把标题(带图片)和内容整合到同一个布局里,完全抛弃系统默认结构:

  1. 创建完整的自定义Dialog布局
    新建dialog_full_custom.xml,包含标题区、内容区和按钮区(可选):

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/dialog_rounded_bg">
    
        <!-- 自定义标题栏(带图片) -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="16dp"
            android:gravity="center_vertical">
    
            <ImageView
                android:id="@+id/iv_dialog_title_icon"
                android:layout_width="28dp"
                android:layout_height="28dp"
                android:src="@drawable/title_icon"/>
    
            <TextView
                android:id="@+id/tv_dialog_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="带图标的标题"
                android:textSize="20sp"
                android:textStyle="bold"
                android:layout_marginStart="12dp"/>
        </LinearLayout>
    
        <!-- 分割线 -->
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#EEEEEE"/>
    
        <!-- 对话框内容区域 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp">
            <!-- 这里放你原来的内容组件 -->
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="这是对话框的内容"/>
        </LinearLayout>
    
        <!-- 按钮区域(可选) -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="end"
            android:padding="12dp">
    
            <Button
                android:id="@+id/btn_cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="取消"
                android:layout_marginEnd="8dp"/>
    
            <Button
                android:id="@+id/btn_confirm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="确认"/>
        </LinearLayout>
    </LinearLayout>
    
  2. 初始化并显示自定义Dialog
    直接创建Dialog实例,设置自定义布局并隐藏系统默认标题:

    Dialog dialog = new Dialog(context);
    // 设置自定义布局
    dialog.setContentView(R.layout.dialog_full_custom);
    // 隐藏系统默认标题
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    
    // 动态修改标题的图片和文本
    ImageView titleIcon = dialog.findViewById(R.id.iv_dialog_title_icon);
    TextView titleText = dialog.findViewById(R.id.tv_dialog_title);
    titleIcon.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.custom_icon));
    titleText.setText("我自己的标题");
    
    // 可选:调整Dialog窗口样式(比如宽度、背景)
    Window window = dialog.getWindow();
    if (window != null) {
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setBackgroundDrawableResource(R.drawable.dialog_rounded_bg);
    }
    
    // 显示Dialog
    dialog.show();
    

小提示

  • 如果需要加载网络图片,可以用Glide等图片加载库替换setImageResource()方法。
  • 第二种方法的灵活性更高,适合需要高度定制Dialog样式的场景。

内容的提问来源于stack exchange,提问作者anshulgoel72

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 23:33:12