自定义对话框上的视图未显示

时间:2015-02-04 09:13:53

标签: android android-layout

我有一个自定义对话框,同时充当waitDialogErrorDialog。这是一个透明的对话框。它确实出现在活动上,但唯一的问题是我在运行时添加的视图没有显示。以下是代码:

MultiPurposeDialog.java

public class MultiPurposeDialog extends Dialog {

    int dialogType;
    String dialogMessage;
    private TextView tvMessage;
    private ImageView ivDialog;
    private Button bDismissDialog;

    public MultiPurposeDialog(Context context) {
        super(context, R.style.TransparentProgressDialog);

        // DIALOG USER_INTERFACE TEMPLATE
        WindowManager.LayoutParams wmLayoutParams = getWindow().getAttributes();
        wmLayoutParams.gravity = Gravity.CENTER_HORIZONTAL;
        getWindow().setAttributes(wmLayoutParams);
        setTitle(null);
        setCancelable(false);
        setOnCancelListener(null);
        LinearLayout layout = new LinearLayout(context);
        layout.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        // DIALOG'S VIEWS
        tvMessage = new TextView(context);
        ivDialog = new ImageView(context);
        bDismissDialog = new Button(context);
        bDismissDialog.setText("Okay");
        bDismissDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        layout.addView(ivDialog);
        layout.addView(tvMessage);
        layout.addView(bDismissDialog);

    }

    @Override
    public void show() {
        super.show();
        switch (dialogType) {
        case Constants.DIALG_CODE_WAIT:


            ivDialog.setImageResource(R.drawable.common_signin_btn_icon_light);
            tvMessage.setText(dialogMessage);
            bDismissDialog.setVisibility(View.GONE);

            // ROTATE WAIT ICON IF IT'S A WAIT DIALOG BOX
            RotateAnimation anim = new RotateAnimation(0.0f, 360.0f,
                    Animation.RELATIVE_TO_SELF, .5f,
                    Animation.RELATIVE_TO_SELF, .5f);
            anim.setInterpolator(new LinearInterpolator());
            anim.setRepeatCount(Animation.INFINITE);
            anim.setDuration(2000);
            ivDialog.setAnimation(anim);
            ivDialog.startAnimation(anim);
            break;
        case Constants.DIALOG_CODE_ERROR:

            ivDialog.setImageResource(android.R.drawable.ic_dialog_alert);
            tvMessage.setText(dialogMessage);
            bDismissDialog.setVisibility(View.VISIBLE);
            break;
        }
    }

    public void setDialogType(int dialogType) {
        this.dialogType = dialogType;
    }

    public void setDialogMessage(String dialogMessage) {
        this.dialogMessage = dialogMessage;
    }

}

Style.xml

 <style name="TransparentProgressDialog" parent="@android:Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowTitleStyle">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:background">@android:color/transparent</item>
    </style>

在MainActivity.java中我正在做这个

multiPurposeDialog = new MultiPurposeDialog(context);
            multiPurposeDialog.setDialogType(Constants.DIALOG_CODE_ERROR);
            multiPurposeDialog.setDialogMessage("Hello");
            multiPurposeDialog.show();

我做错了什么? 感谢

1 个答案:

答案 0 :(得分:3)

  

查看我在运行时添加它没有显示。

因为您没有致电setContentViewDialog中添加由您创建的布局:

尝试按show方法执行:

LinearLayout layout ;
@Override
public void show() {
    this.setContentView(layout);
    super.show();
}

layout类构造函数中使用MultiPurposeDialog添加其他视图