在没有自定义布局文件的情况下,使DialogFragment的消息居中显示

3

我正在尝试在没有使用单独的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();
    }

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

2个回答

1

为了解决这个问题...

编辑:目前我的发现是:如果在设置messageText的对齐方式后再添加另一个builder.show();调用,那么会导致在关闭第一个对话框后立即弹出另一个对话框。

您只需要返回您的对话框即可。

AlertDialog dialog = builder.show(); 
TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
return dialog;     

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

似乎这给了我一个NullPointerException。事实上,每当我尝试对messageText进行任何操作时,我都会得到一个NullPointerException。感谢您的帮助。 - jstein123
嗯...看起来视图只在show()之后而不是create()之后被填充。然而,我尝试了你的代码,它对我有效。我可以在中心看到消息。我在Android版本4.1.1上尝试过。 - prijupaul
我以为你的代码已经可以运行了,现在出了什么问题吗?这段文本被居中对齐在对话框中,这就是你的代码所做的。 - prijupaul
我已经更新了代码,做了一些更改。请检查是否有效?首先进行show操作,然后再进行setLayoutParams或setGravity操作。 - prijupaul
我让它工作了,有点。如果我在设置重力后再次调用 builder.show(),它就会起作用,但这会导致对话框出现两次。我能做些什么吗? - jstein123
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接