自定义Dialog显示异常问题求助
Hey there! I see you're having trouble with your success dialog where text isn't showing fully even though you set android:layout_width="match_parent" on your root layout. Let's break down why this happens and fix it.
Why this happens
By default, Android Dialogs have a fixed maximum width that doesn't take up the full screen width. Even if your layout uses match_parent, the Dialog's window itself is constrained, so your content gets squeezed and truncated.
Solution 1: Adjust Dialog Window Parameters (Most Effective)
You need to explicitly set the Dialog's window width to match the screen. Add this code when you initialize your Dialog:
Dialog successDialog = new Dialog(context); successDialog.setContentView(R.layout.your_success_dialog_layout); Window dialogWindow = successDialog.getWindow(); if (dialogWindow != null) { WindowManager.LayoutParams params = dialogWindow.getAttributes(); // Set width to match the entire screen width params.width = WindowManager.LayoutParams.MATCH_PARENT; // Keep the dialog centered params.gravity = Gravity.CENTER; // Remove default background to eliminate extra padding dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); dialogWindow.setAttributes(params); } successDialog.show();
Solution 2: Tweak Your Layout
If you prefer adjusting the layout instead, you can add a minimum width to your root RelativeLayout to ensure it has enough space for your text:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:minWidth="320dp" <!-- Add this to set a minimum width --> android:padding="16dp"> <!-- Add padding to avoid content sticking to edges --> <ImageView android:id="@+id/img" android:layout_width="100dp" android:layout_height="100dp" android:layout_centerHorizontal="true" android:layout_margin="32dp" android:src="@drawable/ic_done_all_black_24dp" /> <TextView android:id="@+id/OrderSuccessfully" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/img" android:layout_centerHorizontal="true" android:text="@string/order_successfully_placed" android:textAllCaps="true" android:textColor="@android:color/black" android:textSize="20sp" android:textStyle="bold" /> <TextView android:id="@+id/thank" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/OrderSuccessfully" android:layout_centerHorizontal="true" android:layout_margin="8dp" android:text="@string/thank_you" android:textAllCaps="true" android:textColor="@android:color/black" android:textSize="20sp" android:textStyle="bold" /> <Button android:id="@+id/btnOrderSuccessfully" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_below="@+id/thank" android:layout_centerHorizontal="true" android:layout_marginBottom="32dp" android:text="@string/ok" android:textColor="@android:color/white" android:textStyle="bold" android:theme="@style/AppTheme.ButtonSignIn" /> </RelativeLayout>
Bonus Check
If your text strings are longer than expected, make sure your TextViews aren't forced to a single line without proper handling. You can add these attributes to your TextViews if needed:
android:maxLines="2" android:ellipsize="end"
Try out Solution 1 first—it should immediately fix the truncation issue by making the Dialog's window take the full screen width, letting your match_parent layout work as intended.
内容的提问来源于stack exchange,提问作者FPoly HCM - K123v2 Le Xuan Du

