Right justify text in AlertDialog

前端 未结 6 1087
逝去的感伤
逝去的感伤 2020-12-06 04:52

Is it possible to right-justify the text in an AlertDialog\'s title and message?

I am showing Hebrew messages but they are showing up left justified.

6条回答
  •  我在风中等你
    2020-12-06 05:42

    If you want a quick and easy way to do this, I would just create and modify the default alert using AlertDialog.Builder. You can even create a convenience method and class like so:

    /** Class to simplify display of alerts. */
    public class MyAlert {
    
        public static void alert(Context context, String title, String message, OnClickListener listener)
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle("My Title");
            builder.setMessage("My message");
            builder.setPositiveButton("OK", listener);
            AlertDialog dialog = builder.show();
    
            // Must call show() prior to fetching views
            TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
            messageView.setGravity(Gravity.RIGHT);
    
            TextView titleView = (TextView)dialog.findViewById(context.getResources().getIdentifier("alertTitle", "id", "android"));
            if (titleView != null) {
                titleView.setGravity(Gravity.RIGHT);
           }
        }
    }
    

    Of course you could also change the gravity to CENTER for center alignment instead of right or the default left.

提交回复
热议问题