我正在尝试在没有使用单独的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();
调用,我可以让它工作,但这会导致关闭第一个对话框后立即弹出另一个对话框。
messageText
进行任何操作时,我都会得到一个NullPointerException。感谢您的帮助。 - jstein123