自定义对话框显示错误放置的内容

时间:2014-05-24 13:29:46

标签: android dialog alertdialog android-alertdialog

在下面发布的代码中,我是如何自定义Dialog的。但是应该填充Dialog的内容是错误的,或者不在Dialog的各自位置。例如,“报告”一词应与dialog一起放在icon的上半部分,但如图所示,它们位于分隔上部的线下面titleicon的一部分以及dialog的其余部分。如何纠正?

请查看输出图像和代码。

JavaCode:

Dialog reportAlertDialog = new Dialog(MeetingPointFix.this);
            LayoutInflater reportAlertDialogLayoutInflater = LayoutInflater.from(getApplicationContext());
            View reportAlertDialogInflatedView = reportAlertDialogLayoutInflater.inflate(R.layout.report_dialog, null);

            reportAlertDialog.setContentView(reportAlertDialogInflatedView);

            int [] viewsRefsIds = {R.id.reportLocNameValue, R.id.reportLocLatValue, R.id.reportLocLngValue, R.id.reportTimeValue,
                    R.id.reportDateValue, R.id.reportImgTitleValue, R.id.reportImgPathValue
            };
            TextView [] viewsVars = new TextView[viewsRefsIds.length];

            TextView reportAlertDialogMSG = (TextView) reportAlertDialog.findViewById(R.id.reportDialogMessageValue);
            TextView reportAlertDialogTitle = (TextView) reportAlertDialog.findViewById(R.id.reportDialogTitleValue);
            reportAlertDialogMSG.setText(REPORT_ALERT_DIALOG_MSG);
            reportAlertDialogTitle.setText(REPORT_ALERT_DIALOG_TITLE);

            for (int i=0; i<bundleVals.length; i++) {
                viewsVars[i] = (TextView) reportAlertDialog.findViewById(viewsRefsIds[i]);
                viewsVars[i].setText(bundleVals[i]);
            }
            reportAlertDialog.show();

logcat的:

05-24 10:35:32.357: E/AndroidRuntime(5673): FATAL EXCEPTION: main
05-24 10:35:32.357: E/AndroidRuntime(5673): Process: com.example.meetingpointlocator_03, PID: 5673
05-24 10:35:32.357: E/AndroidRuntime(5673): java.lang.RuntimeException: The feature has not been requested
05-24 10:35:32.357: E/AndroidRuntime(5673):     at com.android.internal.policy.impl.PhoneWindow.getDrawableState(PhoneWindow.java:3200)
05-24 10:35:32.357: E/AndroidRuntime(5673):     at com.android.internal.policy.impl.PhoneWindow.setFeatureDrawableResource(PhoneWindow.java:1208)
05-24 10:35:32.357: E/AndroidRuntime(5673):     at android.app.Dialog.setFeatureDrawableResource(Dialog.java:1049)
05-24 10:35:32.357: E/AndroidRuntime(5673):     at com.example.meetingpointlocator_03.MeetingPointFix$4.report(MeetingPointFix.java:296)
05-24 10:35:32.357: E/AndroidRuntime(5673):     at com.example.meetingpointlocator_03.MeetingPointFix$4.onClick(MeetingPointFix.java:240)
05-24 10:35:32.357: E/AndroidRuntime(5673):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
05-24 10:35:32.357: E/AndroidRuntime(5673):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-24 10:35:32.357: E/AndroidRuntime(5673):     at android.os.Looper.loop(Looper.java:136)
05-24 10:35:32.357: E/AndroidRuntime(5673):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-24 10:35:32.357: E/AndroidRuntime(5673):     at java.lang.reflect.Method.invokeNative(Native Method)
05-24 10:35:32.357: E/AndroidRuntime(5673):     at java.lang.reflect.Method.invoke(Method.java:515)
05-24 10:35:32.357: E/AndroidRuntime(5673):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-24 10:35:32.357: E/AndroidRuntime(5673):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-24 10:35:32.357: E/AndroidRuntime(5673):     at dalvik.system.NativeStart.main(Native Method)

输出: enter image description here

2 个答案:

答案 0 :(得分:2)

您需要将标题设置为alerdialog

 reportAlertDialog.setTitle(REPORT_ALERT_DIALOG_TITLE);// takes charactersequence as param

类似地

 reportAlertDialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawabl‌​e.some_icon); 

http://developer.android.com/reference/android/app/Dialog.html

如果您不希望它们出现在对话框内容部分中,则您不需要自定义ImageViewTextView

编辑:

  

尚未请求该功能

Dialog  dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON); // request the feature
dialog.setContentView(R.layout.customlayout);
dialog.setTitle("My Title");
dialog.show();
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher);
// set the icson

答案 1 :(得分:0)

您应该使用AlertDialog.setIcon()AlertDialog.setTitle()来设置对话框的图标和标题。