为什么这个按钮setText无法在我的自定义对话框中显示文字?

时间:2016-05-23 08:26:48

标签: android android-alertdialog android-button

我的代码:

    public class MyBottomDialog extends Dialog {
        LinearLayout container;

        public MyBottomDialog(Context context) {
            super(context);

            container = new LinearLayout(context);
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            container.setLayoutParams(params);
            container.setOrientation(LinearLayout.VERTICAL);
//            container.setBackgroundColor(Color.TRANSPARENT);
            int padding = context.getResources().getDimensionPixelSize(R.dimen.navigation_padding_bottom);
            container.setPadding(padding, padding, padding, padding);

            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(container);
//            setContentView(button);
            setCancelable(true);
            setCanceledOnTouchOutside(true);
            getWindow().setGravity(Gravity.BOTTOM);
//            getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
            getWindow().setWindowAnimations(R.style.BottomDialogAnimation);
        }

        public void addMenuItems(String[] menuItemTitles, int[] menuItemTextColors) {
            for (int i = 0; i < menuItemTitles.length; i++) {
//                Button button = (Button) LayoutInflater.from(getContext()).inflate(R.layout.bottom_menu_item, container, false);
//                button.setText(menuItemTitles[i]);
//                button.setTextColor(menuItemTextColors[i]);
//                container.addView(button);
                addMenuItem(menuItemTitles[i], menuItemTextColors[i]);
            }
        }

        public void addMenuItem(String menuItemTitle, int menuItemTextColor) {
//            Button button = (Button) LayoutInflater.from(getContext()).inflate(R.layout.bottom_menu_item, container, false);

            Button button = produceButton();

            button.setText(menuItemTitle);
            button.setTextColor(menuItemTextColor);
            container.addView(button);
        }

        private Button produceButton() {
            Button button = new Button(getContext());
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            button.setLayoutParams(params);
            button.setGravity(Gravity.CENTER);
//            button.setBackground(getContext().getResources().getDrawable(R.drawable.bottom_menu_button2));

            float[] outerRadii = new float[]{24, 24, 24, 24, 24, 24, 24, 24};
            RoundRectShape rect = new RoundRectShape(outerRadii, null, null);
            ShapeDrawable drawable = new ShapeDrawable(rect);
            drawable.getPaint().setColor(Color.WHITE);
            drawable.getPaint().setStyle(Paint.Style.FILL);
            button.setBackground(drawable);

            return button;
        }

    }

然后我用它:

MyBottomDialog dlg = new MyBottomDialog(this);
dlg.addMenuItems(new String[]{"delete", "cancel"}, new int[]{android.R.color.holo_red_light, android.R.color.holo_blue_light});
dialog.show();

但是对话框中的这两个按钮中没有任何文字,为什么?

0 个答案:

没有答案