简体   繁体   中英

Android programatically set margin to ImageButton in TableRow

I have defined a TableLayout in my Java code. For each row, I'm displaying three ImageButtons. When one of those buttons is pressed, the background color changes. To display it nicely, I have set a padding of 10px.

The problem I'm having now is that if you press two buttons that are next to each other, you don't exactly see a break between the buttons. So I'm wondering, is there a possibility to set a margin on the ImageButtons or an other solution whatshowever?

Try

int leftMargin = 10;
((MarginLayoutParams) imageButton.getLayoutParams()).leftMargin = leftMargin;

EDIT: If you don't use ImageButton defined in xml, you have to set LayoutParams like this:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
params.leftMargin = 10;
b.setLayoutParams(params);

Here I assume you use LinearLayout in your list item.

确保使用LayoutParams作为控件所在的容器。在您的情况下,按钮位于TableRow因此如果使用任何其他类型的参数,则使用TableRow.LayoutParams ,控件将根本不显示。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM