2

我正在尝试将中心重心添加到对话框片段的消息中,而没有用于自定义布局的单独 XML 文件。我已经尝试获取 TextView 并设置它的严重性,但它似乎没有做任何事情:

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Replace current sound");
        builder.setMessage("Choose a mp3 or ogg file or restore the default sound.\nThe current sound is:\n"+currentSound)
               .setPositiveButton("Choose new sound", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                    // This is the Choose File dialog (uses aFileDialog library)
                    // to get the file replacement
                    FileChooserDialog dialog1 = new FileChooserDialog(context);
                    dialog1.show();
                    dialog1.addListener(new OnFileSelectedListener() {
                        @Override
                        public void onFileSelected(Dialog source, File file) {
                            // TODO Auto-generated method stub
                            source.hide();
                            Log.e("App Debugging", file.getAbsolutePath());
                            String fileReplacement = file.getAbsolutePath();

                        }

                        @Override
                        public void onFileSelected(Dialog source, File folder,
                                String name) {
                            // TODO Auto-generated method stub

                        }
                    });
                   }
               })
               .setNegativeButton("Restore default", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {

                   }
               });
        AlertDialog dialog = builder.show(); 
        TextView messageText = TextView)dialog.findViewById(android.R.id.message);
        messageText.setGravity(Gravity.CENTER);
        // Create the AlertDialog object and return it
        return builder.create();
    }

builder.show();编辑:到目前为止,这是我的发现:如果我在设置重力后添加另一个调用,我可以让它工作messageText,但这会导致在关闭第一个对话框后立即弹出另一个对话框。

4

2 回答 2

1

为了解决这个问题...

builder.show();编辑:到目前为止,这是我的发现:如果在设置 ' 的重力后添加另一个调用,我可以让它工作messageText,但这会导致在关闭第一个对话框后立即弹出另一个对话框。

你只需要返回你的对话框

AlertDialog dialog = builder.show(); 
TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
return dialog;     
于 2014-12-03T13:19:28.817 回答
1

你能这样试试吗

AlertDialog dialog = builder.show(); 
TextView messageText = (TextView)dialog.findViewById(android.R.id.message); 
FrameLayout.LayoutParams p = (FrameLayout.LayoutParams) messageText.getLayoutParams();
p.gravity = Gravity.CENTER;
messageText.setLayoutParams(p);
于 2013-09-23T02:44:49.100 回答