【问题标题】:Right justify text in AlertDialog右对齐 AlertDialog 中的文本
【发布时间】:2011-09-02 03:13:06
【问题描述】:

是否可以右对齐 AlertDialog 的标题和消息中的文本?

我正在显示希伯来语消息,但它们显示为左对齐。

【问题讨论】:

标签: android android-alertdialog justify


【解决方案1】:

这是一个老问题,但有一个非常简单的解决方案。假设您使用的是MinSdk 17,您可以将其添加到您的styles.xml

<style name="AlertDialogCustom" parent="ThemeOverlay.AppCompat.Dialog.Alert">
    <item name="android:layoutDirection">rtl</item>
</style>

而在AlertDialog.Builder 中你只需要在构造函数中指定这个AlertDialogCustom

new AlertDialog.Builder(this, R.style.AlertDialogCustom)
                    .setTitle("Your title?")
                    .show();

【讨论】:

  • 我非常喜欢您的解决方案,它改变了包括按钮在内的所有内容,但没有显示标题和消息文本,有什么提示吗?
  • 使用parent="ThemeOverlay.AppCompat.Dialog.Alert" 在自定义警报对话框中设置您的主题。
【解决方案2】:

据我所知,从AlertDialogAlertController 的代码中,您无法访问负责消息和标题的TextViews。

您可以使用反射访问 AlertDialog 实例中的mAlert 字段,然后再次使用反射访问mAlertmMessagemTitle 字段。虽然我不推荐这种方法,因为它依赖于内部(将来可能会改变)。


作为另一个(可能更好的)解决方案,您可以通过AlertDialog 构造函数应用自定义主题。这将允许您右对齐该对话框中的所有 TextViews。

     protected AlertDialog (Context context, int theme)

这应该是更简单、更健壮的方法。


这里是一步一步的说明:

第 1 步。 创建 res/values/styles.xml 文件。这里是它的内容:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="RightJustifyTextView" parent="@android:style/Widget.TextView">
        <item name="android:gravity">right|center_vertical</item>
    </style>

    <style name="RightJustifyDialogWindowTitle" parent="@android:style/DialogWindowTitle" >
         <item name="android:gravity">right|center_vertical</item>
    </style>

    <style name="RightJustifyTheme" parent="@android:style/Theme.Dialog.Alert">
        <item name="android:textViewStyle">@style/RightJustifyTextView</item>       
        <item name="android:windowTitleStyle">@style/RightJustifyDialogWindowTitle</item>       
    </style>    

</resources>

第 2 步。 创建 RightJustifyAlertDialog.java 文件。这里是它的内容:

public class RightJustifyAlertDialog extends AlertDialog
{
    public RightJustifyAlertDialog(Context ctx)
    {
        super(ctx,  R.style.RightJustifyTheme);
    }
}

第 3 步。 使用RightJustifyAlertDialog 对话框:

AlertDialog dialog = new RightJustifyAlertDialog(this);
dialog.setButton("button", new OnClickListener()
{           
    public void onClick(DialogInterface arg0, int arg1)
    {

    }
});
dialog.setTitle("Some Title");
dialog.setMessage("Some message");

dialog.show();

第 4 步。 检查结果:

【讨论】:

  • 我必须定义所有主题还是有一个可以建立的基线?我以前没用过主题。
  • 您可以继承默认主题并添加您的更改。例如,在您的styles.xml 中,您可以创建一个名为CustomTheme 的新主题并像这样启动它:&lt;style name="CustomTheme" parent="android:Theme.Light"&gt;。然后你只需要添加你想要改变的项目。
  • 我尝试做 mydialog = new AlertDialog.Builder(this,R.style.MyDialogTheme) 但它声称构造函数不存在。
  • 好的 - 谢谢。我还将上传我创建的迷你项目并提供链接。
  • 这不适用于 AppCompat 它们和对话框
【解决方案3】:

如果您想要一种快速简便的方法来执行此操作,我只需使用 AlertDialog.Builder 创建和修改默认警报。你甚至可以像这样创建一个方便的方法和类:

/** 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);
       }
    }
}

当然,您也可以将重力更改为 CENTER 以实现居中对齐,而不是右对齐或默认左对齐。

【讨论】:

  • 效果很好,但我遇到了另一个问题。标题和文本对齐很好,但按钮不是。如何通过 id 获取按钮?
【解决方案4】:

为此苦苦挣扎了一个小时(上面的解决方案对我不起作用),我设法将 AlertDialog 的标题和消息向右对齐,而没有覆盖任何样式,只需更改重力和布局参数即可。

在 DialogFragment 中:

@Override
public void onStart() {
    super.onStart();

    // Set title and message
    try {
        alignDialogRTL(getDialog(), this);
    }
    catch (Exception exc) {
        // Do nothing
    }
}

实际功能:

public static void alignDialogRTL(Dialog dialog, Context context) {
    // Get message text view
    TextView message = (TextView)dialog.findViewById(android.R.id.message);

    // Defy gravity
    message.setGravity(Gravity.RIGHT);

    // Get title text view
    TextView title = (TextView)dialog.findViewById(context.getResources().getIdentifier("alertTitle", "id", "android"));

    // Defy gravity (again)
    title.setGravity(Gravity.RIGHT);

    // Get title's parent layout
    LinearLayout parent = ((LinearLayout)Title.getParent());

    // Get layout params
    LinearLayout.LayoutParams originalParams = (LinearLayout.LayoutParams)parent.getLayoutParams();

    // Set width to WRAP_CONTENT
    originalParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;

    // Defy gravity (last time)
    originalParams.gravity = Gravity.RIGHT |  Gravity.CENTER_VERTICAL;

    // Set updated layout params
    parent.setLayoutParams(originalParams);
}

【讨论】:

    【解决方案5】:

    kotlinish 方式:

    val alertDialog = alertDialogBuilder.create()
    
    alertDialog.setOnShowListener {
    
       alertDialog.window?.decorView?.layoutDirection = View.LAYOUT_DIRECTION_RTL
                                   }
    
    alertDialog.show()
    

    【讨论】:

      【解决方案6】:

      为了将警报对话框的布局方向设置为 RTL,您可以使用 OnShowListener 方法。 设置完 title , message , .... 使用这个方法。

      dialog = alertdialogbuilder.create();
      
              dialog.setOnShowListener(new DialogInterface.OnShowListener() {
                  @Override
                  public void onShow(DialogInterface dlg) {
                      dialog.getButton(Dialog.BUTTON_POSITIVE).setTextSize(20); // set text size of positive button
                      dialog.getButton(Dialog.BUTTON_POSITIVE).setTextColor(Color.RED); set text color of positive button
      
                      dialog.getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL); // set title and message direction to RTL
                  }
              });
              dialog.show();
      

      【讨论】:

        【解决方案7】:

        你只需要在 res/values/styles 中创建一个样式标签并编写:

        <style name="alertDialog LayoutDirection">
        <item name="android:layoutDirection">rtl</item>
        

        然后在您的活动中初始化警报对话框后添加:

        alertDialog.setView(R.style.alertDialog);
        

        例如:

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
                alertDialog.setMessage("حذف بشه؟")
                        .setCancelable(true);
                alertDialog.setPositiveButton("آره", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this, "yes", Toast.LENGTH_SHORT).show();
                    }
                });
                alertDialog.setNegativeButton("نه", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this, "nooo", Toast.LENGTH_SHORT).show();
                    }
                });
        
                alertDialog.show();
                alertDialog.setView(R.style.alertDialog);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-04-06
          • 1970-01-01
          • 2012-11-16
          • 2014-07-23
          • 2012-06-09
          相关资源
          最近更新 更多