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

Android AlertDialog自定义视图在部分设备显示截断问题求助

自定义AlertDialog在Android12设备显示截断的解决方法

问题现象

游戏中使用自定义视图的AlertDialog,多数设备显示正常,但在三星Galaxy A73 5G(Android12)上出现对话框内容截断的情况:

  • 正常显示:对话框完整展示标题、分割线和全部文本内容,底部确认按钮正常显示
  • 截断显示:对话框右侧文本被裁切,底部确认按钮部分或完全不可见

问题原因

  1. 布局里ScrollView和内部TextView用了wrap_content宽度,部分设备上内容会超出对话框可用范围,导致截断。
  2. 加载布局时直接传null作为父容器,布局无法正确继承对话框的布局参数,适配逻辑失效。
  3. Android12对对话框窗口尺寸的约束有调整,原有样式没做适配。

解决步骤

1. 调整布局文件(message_dialog.xml)

把ScrollView和内部TextView的宽度改成match_parent,给ScrollView加最大高度限制,同时补全文本底部内边距:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="30dp"
        android:paddingTop="14dp"
        android:paddingBottom="9dp"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/dark_blue" />

    <!-- 宽度改为match_parent,添加最大高度防止内容过长 -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxHeight="400dp">

        <!-- 宽度改为match_parent,让文本自动换行,补全底部内边距 -->
        <TextView
            android:id="@+id/message_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:paddingRight="10dp"
            android:paddingBottom="10dp"
            android:textColor="@color/grey_text_color"
            android:textSize="16sp" />
    </ScrollView>
</LinearLayout>

2. 优化布局加载逻辑(Java代码)

加载布局时传入父容器参数,确保布局能正确继承对话框的约束,同时适配Html解析的版本差异:

AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.CustomDialogStyle);
// 传入false避免自动添加到父容器,同时让布局继承正确的LayoutParams
View view = activity.getLayoutInflater().inflate(R.layout.message_dialog, null, false);

String messageText = String.format(Strings.daily_star_bonus_message, Configs.DAILY_BONUS_TIPS);

TextView title = view.findViewById(R.id.dialog_title);
title.setText("Тестовый Диалог 1");

TextView message = view.findViewById(R.id.message_text);
// Android N及以上指定解析模式,避免兼容性问题
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    message.setText(Html.fromHtml(messageText, Html.FROM_HTML_MODE_LEGACY));
} else {
    message.setText(Html.fromHtml(messageText));
}

builder.setCancelable(true);
builder.setView(view);

builder.setPositiveButton(Strings.button_ok, (dialogInterface, i) -> dialogInterface.dismiss());

builder.show();

3. 适配Android12的对话框样式(styles.xml)

给自定义对话框样式添加窗口尺寸相关属性,兼容Android12的约束:

<style name="CustomDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/dark_blue</item>
    <!-- 适配Android12的对话框最小宽度规范 -->
    <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
    <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
</style>

额外提示

如果调整后仍有问题,可以尝试给setView方法手动设置内边距,数值根据实际显示效果调整:

builder.setView(view, 40, 20, 40, 20);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 05:35:29