Alert Dialog标题被截断显示省略号,如何在strings.xml中设置标题尺寸?是否存在标题仅支持2行显示的限制?
Hey there, let's work through your AlertDialog title truncation problem! You're right to suspect the default line limit—most stock AlertDialog title TextViews are set to a maximum of 2 lines, which is why longer titles get cut off with ellipses. Let's go through two solid solutions to fix this, plus clarify how to handle text sizes properly (since strings.xml isn't the right place for that—we'll cover that too).
1. Modify Title Properties Directly in Code
You already adjusted the message TextView in your code, so we can do the same for the title. Right after calling dialog.show(), add code to find the title TextView and update its max lines and text size:
private fun remindAcceptOrders(newStatus: OrderStatus, orderDetails: OrderDetails) { val dialog = AlertDialog.Builder(requireContext(), R.style.AlertDialogTheme) .setTitle(getString(R.string.acceptMessage2)) .setPositiveButton(R.string.acceptorder) { dialog, _ -> showNewStatusConfirmationPopup(ACCEPTED, orderDetails) dialog.dismiss() } .setNegativeButton(R.string.exit) { dialog, _ -> dialog.dismiss() navc!!.navigate(R.id.action_orderDetailsFragment_to_orderListFragment) } .setMessage(getString(R.string.acceptMessage1)) .create() dialog.show() // Adjust message TextView (your existing code) val messageTextView = dialog.findViewById<TextView>(android.R.id.message) messageTextView?.apply { textSize = 15f maxLines = 3 } // Add this to fix title truncation and adjust size val titleTextView = dialog.findViewById<TextView>(androidx.appcompat.R.id.alertTitle) titleTextView?.apply { maxLines = Int.MAX_VALUE // Allow unlimited lines, or set to 3/4 as needed textSize = 18f // Adjust to your desired size ellipsize = null // Remove ellipsis if you want full text (optional) } }
Note: The title's ID might vary slightly depending on your support library—androidx.appcompat.R.id.alertTitle works for most modern AppCompat setups. If that doesn't work, try android.R.id.title instead.
2. Set Up a Custom AlertDialog Theme (For Reusability)
If you want this title behavior to apply to all your AlertDialogs, defining a custom theme is better than repeating code every time. Here's how:
Step 1: Add a Title Style in styles.xml
First, create a style for the title TextView:
<style name="AlertDialogTitleStyle" parent="TextAppearance.AppCompat.Dialog.Alert.Title"> <item name="android:maxLines">4</item> <!-- Set your desired max lines --> <item name="android:textSize">@dimen/alert_dialog_title_size</item> <!-- Reference dimension --> <item name="android:ellipsize">end</item> <!-- Keep ellipsis if needed, or set to "none" --> </style>
Step 2: Define the Dimension in dimens.xml
Since strings.xml is for string resources only, store your text size in res/values/dimens.xml:
<dimen name="alert_dialog_title_size">18sp</dimen>
Step 3: Update Your AlertDialog Theme
Modify your existing AlertDialogTheme to use this title style:
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert"> <!-- Your existing theme attributes --> <item name="android:windowTitleStyle">@style/AlertDialogTitleStyle</item> </style>
Now every AlertDialog using R.style.AlertDialogTheme will automatically apply these title settings—no extra code needed!
Why strings.xml Isn't for Text Sizes
Just to clarify: strings.xml is designed solely for storing string content (like your acceptMessage2 text). Text sizes, margins, and other dimension values belong in dimens.xml—this keeps your resources organized and makes it easier to adjust sizes for different screen densities.
内容的提问来源于stack exchange,提问作者Ugur d

