the*_*itz 15 android justify android-alertdialog
是否可以在AlertDialog的标题和消息中对文本进行右对齐?
我正在展示希伯来语的消息,但它们显示左对齐.
n8t*_*8tr 17
如果您想要一种快速简便的方法,我只需使用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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
当然,您也可以将重力更改为CENTER以进行居中对齐而不是右侧或默认左侧.
ysh*_*hak 16
这是一个老问题,但有一个非常简单的解决方案.假设您正在使用MinSdk 17,您可以将其添加到您的styles.xml:
<style name="AlertDialogCustom" parent="Theme.AppCompat.Dialog.Alert">
<item name="android:layoutDirection">rtl</item>
</style>
Run Code Online (Sandbox Code Playgroud)
在AlertDialog.Builder你只需要AlertDialogCustom在构造函数中指定:
new AlertDialog.Builder(this, R.style.AlertDialogCustom)
.setTitle("Your title?")
.show();
Run Code Online (Sandbox Code Playgroud)
ina*_*ruk 13
据我可以从代码中看到的AlertDialog和AlertController你不能访问TextView负责信息和标题秒.
您可以使用反射到达mAlertAlertDialog实例中的字段,然后再次使用反射来访问mMessage和mTitle字段mAlert.虽然我不推荐这种方法,因为它依赖于内部(可能在将来发生变化).
作为另一种(可能更好)的解决方案,您可以通过AlertDialog构造函数应用自定义主题.这将允许您右对齐TextView该对话框中的所有s.
protected AlertDialog (Context context, int theme)
Run Code Online (Sandbox Code Playgroud)
这应该是更容易和更强大的方法.
这是一步一步的说明:
步骤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>
Run Code Online (Sandbox Code Playgroud)
第2步.创建RightJustifyAlertDialog.java文件.其内容如下:
public class RightJustifyAlertDialog extends AlertDialog
{
public RightJustifyAlertDialog(Context ctx)
{
super(ctx, R.style.RightJustifyTheme);
}
}
Run Code Online (Sandbox Code Playgroud)
第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();
Run Code Online (Sandbox Code Playgroud)
第4步.检查结果:

| 归档时间: |
|
| 查看次数: |
14783 次 |
| 最近记录: |