如何修复AlertDialog不包装在所有设备上的布局

我尝试将alertDialog主题设置为R.style.Theme_MaterialComponents_Light_Dialog_Alert

虽然可以将alertDialog的内容包装到在huawei y9(Android Pie)上放大的布局中,但没有将其包装在lenovo api 19上

我尝试将LayoutParams设置为wrap_content,但仍然无济于事:

val inflater = LayoutInflater.from(mCtx)
    val viewInflater = inflater.inflate(R.layout.popup_menu,null)
    val window = AlertDialog.Builder(mCtx,R.style.Theme_MaterialComponents_Light_Dialog_Alert)
            .setView(viewInflater)
            .setCancelable(false)
    alertD = window.create()
    //alertD.window?.setLayout(WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT)
    alertD.show()

因此,我需要AlertDialog将其内容包装到所有手机上的布局中。

guoguo337 回答:如何修复AlertDialog不包装在所有设备上的布局

尝试创建自定义对话框样式,并以百分比形式添加宽度和高度。

min_Width_minor为:

  

平台为对话框宽度时所需的最小大小   沿短轴(即屏幕为纵向)。这可能是   分数或维度。

Min_witdh_major是:

  

平台为对话框宽度时所需的最小大小   沿短轴(即屏幕为纵向)。这可能是   分数或维度。

尝试下面的示例。

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">

    <item name="android:windowMinWidthMajor">80%</item>
    <item name="android:windowMinWidthMinor">80%</item>
    <item name="windowFixedHeightMinor">80%</item>
    <item name="windowFixedHeightMajor">80%</item>

    <item name="colorAccent">#FFC107</item>
    <!-- Used for the title and text -->
    <item name="android:textColorPrimary">#FFFFFF</item>
    <!-- Used for the background -->
    <item name="android:background">#4CAF50</item>

</style>


 AlertDialog.Builder myAlert = new AlertDialog.Builder(this,R.style.AlertDialogCustom);
        myAlert.setTitle("Title");
        myAlert.setMessage("1298201");
        myAlert.show();
本文链接:https://www.f2er.com/3160019.html

大家都在问