如何以编程方式绘制宽度为MATCH_PARENT的按钮?

时间:2019-02-20 13:20:40

标签: java android android-layout button

在这里,我尝试以编程方式简单地绘制一个按钮,该按钮的宽度应与parent匹配,但是当我将其设置为match_parent时,什么也没有发生,并且我的按钮只是消失了,但是如果我设置了某个值,则说明成功创建了500或700按钮。

private void drawButtons(Canvas c, RecyclerView.ViewHolder viewHolder) {
        //float buttonWidthWithoutPadding = buttonWidth - 20;
        float corners = 16;

        View itemView = viewHolder.itemView;
        Paint p = new Paint();

        RectF leftButton = new RectF(itemView.getLeft(), itemView.getTop(), itemView.getLeft() + MATCH_PARENT, itemView.getBottom());
        p.setColor(Color.BLACK);
        c.drawRoundRect(leftButton, corners, corners, p);
        drawText("Hours Worked:"+" "+"", c, leftButton, p);
    }
    private void drawText(String text, Canvas c, RectF button, Paint p) {
        float textSize = 60;
        p.setColor(Color.WHITE);
        p.setAntiAlias(true);
        p.setTextSize(textSize);

        float textWidth = p.measureText(text);
        c.drawText(text, button.centerX()-(textWidth/2), button.centerY()+(textSize/2), p);
    }

0 个答案:

没有答案