AlertDialog框中的中心消息

时间:2018-05-02 03:01:08

标签: java android

我为AlertDialog框创建了自定义标题。但我无法集中信息。它总是左对齐。

    TextView vehicle_no = (TextView) findViewById(R.id.vehicle_no);
    String ScoreTitle = getResources().getString(R.string.AlertTitle);

    AlertDialog.Builder alrtbuilder = new AlertDialog.Builder(this);
    Context mContext = alrtbuilder.getContext();
    LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
    View mView = mLayoutInflater.inflate(R.layout.simple_dialog, null);
    TextView mTextView = (TextView) mView.findViewById(R.id.title_text);
    mTextView.setText(ScoreTitle);

AlertDialog框有正面和负面按钮

    alrtbuilder.setCustomTitle(mView);
    alrtbuilder.setMessage(vehicle_no.getText().toString().toUpperCase()+" "+"?")
               .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    insert_timecard();
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            }).show;

3 个答案:

答案 0 :(得分:1)

在您的布局android:gravity="center"中将TextView添加到标识为R.id.title_text的{​​{1}}。

R.layout.simple_dialog

要将默认消息 <TextView android:id="@+id/title_text" android:layout_width="match_parent" android:gravity="center" android:layout_height="wrap_content" /> 置于中心位置,您必须在TextView之后从对话框中获取TextView

show()

我不知道它是否是一个好的方法,但您也可以将自定义视图设置为整个对话框,然后可以轻松处理事情。

答案 1 :(得分:1)

添加:

  

mTextView.setGravity(Gravity.CENTER_HORIZONTAL);

TextView vehicle_no = (TextView) findViewById(R.id.vehicle_no);
String ScoreTitle = getResources().getString(R.string.AlertTitle);

AlertDialog.Builder alrtbuilder = new AlertDialog.Builder(this);
Context mContext = alrtbuilder.getContext();
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
View mView = mLayoutInflater.inflate(R.layout.simple_dialog, null);
TextView mTextView = (TextView) mView.findViewById(R.id.title_text);
mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
mTextView.setText(ScoreTitle);

这应解决:

答案 2 :(得分:1)

如何添加此内容?

TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);