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 に答える