将背景设置为 table 中的按钮

setBackground to buttons in a table

我在 ViewFlipper 中的 table 中有一系列按钮,因此我可以对 table 进行分页。如果我不设置它们的背景,它们将保持彼此之间的边距。但是我需要"make them pretty",所以我给按钮添加了一个渐变。但现在他们不再保持利润。

我以编程方式添加它们,循环按钮必须具有的文本的 ArrayList,将它们添加到 table 的正确行中,等等。像这样:

final Button btn = new Button(this);
btn.setText(array.get(i));
btn.setTextSize(10);

btn.setOnClickListener(new View.OnClickListener() 
{
    @Override
    public void onClick(View v) 
    {
        Log.i("TAG", "Botón pulsado: " + texto);
        goToModule(texto);
    }
});
btn.setHeight(height);
btn.setWidth(width);
btn.setOnTouchListener(parentListener);
btn.post(new Runnable() 
{
    @Override
    public void run() 
    {
        btn.setCompoundDrawablesWithIntrinsicBounds(null,getDrawableForText(texto),null, null);
        btn.setPadding(0,35,0,0);
        int[] colors = {Color.parseColor("#008000"),Color.parseColor("#ADFF2F")};
        GradientDrawable gd = new GradientDrawable(
        GradientDrawable.Orientation.TL_BR, colors);
        gd.setCornerRadius(30f);
        btn.setBackground(gd);
    }
});
row1.addView(btn);

屏幕上的结果是这样的:

有没有办法让按钮看起来像没有渐变的按钮,我的意思是,尊重彼此之间的边距,让它们看起来更像一个合适的 table?

谢谢。

尝试在 xml 中将边距更改为填充。

尝试为按钮添加边距

 TableRow.LayoutParams params = new TableRow.LayoutParams(
            TableRow.LayoutParams.WRAP_CONTENT,
            TableRow.LayoutParams.WRAP_CONTENT
    );
    params.setMargins(10, 10, 10, 10);
    btn.setLayoutParams(params);