android对话框中的中心消息

时间:2011-02-10 06:41:05

标签: java android

我希望对话框中的消息文本居中对齐。

8 个答案:

答案 0 :(得分:160)

当然,您始终可以设置原始文本视图的重力。这使您不必担心格式化和填充。

例如

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
builder.setMessage("Message");
builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.show();

// Must call show() prior to fetching text view
TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);

答案 1 :(得分:74)

创建自己的TextView对象,然后将其作为View:

提供给弹出构建器
AlertDialog.Builder popupBuilder = new AlertDialog.Builder(this);
TextView myMsg = new TextView(this);
myMsg.setText("Central");
myMsg.setGravity(Gravity.CENTER_HORIZONTAL);
popupBuilder.setView(myMsg);

您可以控制所有其他文本参数(样式,颜色,大小......)。要控制边距,可以以编程方式创建LinearLayout,设置LayoutParams,然后将TextView放入其中。

答案 2 :(得分:11)

以Chase的答案为基础,以下是如何将标题集中在一起。我认为这是最简单的方法。为什么android默认不居中或者使它成为一个简单的构造函数参数超出我的范围。

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.CENTER);

TextView titleView = (TextView)dialog.findViewById(context.getResources().getIdentifier("alertTitle", "id", "android"));
if (titleView != null) {
    titleView.setGravity(Gravity.CENTER);
}

答案 3 :(得分:6)

我们可以像使用一样。

public static void showAlert(Activity activity, String message) {

        TextView title = new TextView(activity);
        // You Can Customise your Title here 
        title.setText("Your Title Here");
        //title.setBackgroundColor(Color.DKGRAY);
        title.setPadding(10, 10, 10, 10);
        title.setGravity(Gravity.CENTER);
        title.setTextColor(Color.WHITE);
        title.setTextSize(20);


        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setCustomTitle(title);
        //builder.setIcon(R.drawable.alert_36);

        builder.setMessage(message);

        builder.setCancelable(false);
        builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();

            }

        });

        AlertDialog alert = builder.show();
        TextView messageText = (TextView)alert.findViewById(android.R.id.message);
        messageText.setGravity(Gravity.CENTER);
        messageText.setTextColor(Color.RED);

    }

答案 4 :(得分:3)

只需使用该方法,您的对话框标题和消息就会显示在中心位置:

public static void openDialog(Context context,String message){

    TextView title = new TextView(context);
    // You Can Customise your Title here
    title.setText("Information Message");
    title.setBackgroundColor(Color.BLACK);
    title.setPadding(10, 15, 15, 10);
    title.setGravity(Gravity.CENTER);
    title.setTextColor(Color.WHITE);
    title.setTextSize(22);

    AlertDialog alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setCustomTitle(title);
    alertDialog.setMessage(message);

    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    alertDialog.show();

    // You Can Customise your Message here
    TextView messageView = (TextView) alertDialog
            .findViewById(android.R.id.message);
    messageView.setGravity(Gravity.CENTER);

}

答案 5 :(得分:3)

你可以试试这段代码

public void Info(){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Info Aplication");
    builder.setIcon(R.drawable.info_adp);
    builder.setMessage("Jakarta Hospital");
    builder.setCancelable(false);
    builder.setPositiveButton("Exit", null);
    AlertDialog dialog = builder.show();
    TextView messageView = (TextView)dialog.findViewByI(android.R.id.message);
    messageView.setGravity(Gravity.CENTER);
}

答案 6 :(得分:2)

在TextView中尝试:

android:gravity = "center_vertical|center"

答案 7 :(得分:2)

不使用textview。 为我工作。

styles.xml

<style name="CustomAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:gravity">center</item>
    </style>

警报对话框

final AlertDialog builder = new AlertDialog.Builder(activity, 
R.style.CustomAlertDialog)