【问题标题】:Centering the message of a DialogFragment without a custom layout file在没有自定义布局文件的情况下将 DialogFragment 的消息居中
【发布时间】:2014-12-03 22:14:40
【问题描述】:

我正在尝试将重心添加到对话框片段的消息中,而没有用于自定义布局的单独 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(); 调用,我可以让它工作,但这会导致在关闭第一个对话框后立即弹出另一个对话框。

【问题讨论】:

    标签: android android-dialogfragment


    【解决方案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。感谢您的帮助。
    • hmm.. 看起来视图仅在 show() 之后而不是在 create() 之后填充。但是,我尝试了您的代码,它对我有用。我可以在中心看到味精。我在 Android 4.1.1 版本中尝试过
    • 我认为您的代码正在运行。现在有什么问题?文本与对话框的中心对齐。这就是你的代码所做的。
    • 它似乎对我没有任何作用。我不确定为什么它对你有用,而不是对我有用,也许我做错了什么。它应该在return builder.create() 之前,对吧?
    • 我已经对代码进行了一些更改。检查这是否有效?先做一个表演,然后 setlayoutparams 或 setGravity。
    【解决方案2】:

    为了解决这个问题……

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

    你只需要返回你的对话框

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-26
      • 2012-09-22
      • 2016-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多