如何将drawable放在上面以编程方式生成的单选按钮上

时间:2017-04-20 16:03:13

标签: android

我从网络服务中检索测验,我希望将其呈现为this

我正在尝试以编程方式生成RadioButtons。

我有图片和文字测验,我使用rg.setOrientation();显示测验h​​orizo​​ntaly / verticaly取决于quizz类型。

我尝试使用 Drawable 添加图片,但我无法将RadioButton置于图片下方居中。我只能得到this。 我无法将文本置于中心

我写的是代码。

            final RadioGroup rg = (RadioGroup) v.findViewById(R.id.radio_group);
        final int[] try_answer = {NUMBER_OF_TRY_MAX};


        if (question.getImage()) {

            if (!isTablet) {
                rg.setOrientation(LinearLayout.VERTICAL);
            } else {
                rg.setOrientation(LinearLayout.HORIZONTAL);
            }
                 rg.setGravity(Gravity.CENTER_HORIZONTAL);

            for (int i = 0; i < answers.size(); i++) {
                Answer answer = answers.get(i);
                RadioButton button = new RadioButton(getContext());


                db.open();
                Image img = db.getImageWithURL(answer.getPicture());
                db.close();
                String localName = img.getImage();
                String file = path + localName;
                Bitmap bitmap = BitmapFactory.decodeFile(file);


                Drawable d = new BitmapDrawable(getContext().getResources(), Bitmap.createScaledBitmap(bitmap, 300, 300, true));


                d.setBounds(0, 0, 300, 300);
                button.setCompoundDrawablesWithIntrinsicBounds(null,d,null,null);
                button.setTextColor(Color.WHITE);
                button.setTag(answer.right_answer);
                button.setGravity(Gravity.LEFT);
                button.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

                button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
                int[][] states = new int[][]{
                        new int[]{ android.R.attr.state_enabled}, // enabled
                        new int[]{-android.R.attr.state_enabled}, // disabled
                        new int[]{-android.R.attr.state_checked}, // unchecked
                        new int[]{ android.R.attr.state_checked}, // checked
                        new int[]{ android.R.attr.state_pressed}  // pressed
                };

                int[] colors = new int[]{
                        Color.WHITE,
                        Color.GRAY,
                        Color.WHITE,
                        Color.BLUE,
                        Color.BLUE
                };
                ColorStateList myList = new ColorStateList(states, colors);
                button.setButtonTintList(myList);
                button.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (try_answer[0] > 0) {

                            if ((Boolean) v.getTag()) {
                                displayRightAnswerImage(rg);
                                question.setAnswered(true);
                                checkIfAllQuestionsAnswered();
                            } else {
                                try_answer[0]--;

                                if (try_answer[0] == 0) {
                                    displayRightAnswerImage(rg);
                                    question.setAnswered(true);
                                    checkIfAllQuestionsAnswered();

                                } else {
                                    int selected = rg.getCheckedRadioButtonId();
                                    RadioButton rb = (RadioButton)  v.findViewById(selected);
                                    Drawable[] drawables = rb.getCompoundDrawables();
                                    Drawable draw = drawables[1];
                                    draw.setAlpha(125);


                                    Toast.makeText(getContext(), "il reste " + try_answer[0] + " essais", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    }

                });

               rg.addView(button);

            }

        } else {
            rg.setOrientation(LinearLayout.VERTICAL);
            for (int i = 0; i < answers.size(); i++) {
                Answer answer = answers.get(i);
                final RadioButton button = new RadioButton(getContext());
                rg.addView(button);
                button.setText(answer.getTitle());
                button.setTextColor(Color.WHITE);
                button.setTag(answer.right_answer);
                button.setGravity(Gravity.CENTER_HORIZONTAL);
                button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (try_answer[0] > 0) {
                            if ((Boolean) v.getTag()) {
                                displayRightAnswer(rg);
                                question.setAnswered(true);
                                checkIfAllQuestionsAnswered();

                            } else {
                                try_answer[0]--;

                                if (try_answer[0] == 0) {
                                    displayRightAnswer(rg);
                                    question.setAnswered(true);
                                    checkIfAllQuestionsAnswered();
                                } else {

                                    Toast.makeText(getContext(), "il reste " + try_answer[0] + " essais", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    }

                });
            }
        }

    return v;

如何使显示看起来与第一个屏幕类似。

是否可以或应该使用除radiobutton之外的其他组件来正确渲染它?

1 个答案:

答案 0 :(得分:0)

使用Relativelayout.LayoutParams对象并向其添加规则。查看this