Android以编程方式在TableRow中为ImageButton设置边距

Han*_*ore 3 android margin imagebutton tablerow

我在Java代码中定义了一个TableLayout.对于每一行,我显示三个ImageButtons.按下其中一个按钮时,背景颜色会发生变化.为了很好地显示它,我设置了10px的填充.

我现在遇到的问题是,如果你按下彼此相邻的两个按钮,你就不会看到按钮之间的中断.所以我想知道,是否有可能在ImageButtons或其他解决方案上设置保证金?

ern*_*azm 11

尝试

int leftMargin = 10;
((MarginLayoutParams) imageButton.getLayoutParams()).leftMargin = leftMargin;
Run Code Online (Sandbox Code Playgroud)

编辑: 如果你不使用ImageButton在xml中定义,你必须这样设置LayoutParams:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
params.leftMargin = 10;
b.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)

在这里,我假设您LinearLayout在列表项中使用.