I am facing issue when setting the layout_margin
programatically for the dynamically created ImageView
inside a GridLayout
. I have tried the below code :
GridLayout gridLayout = (GridLayout) findViewById(R.id.idGridLayout);
gridLayout.setColumnCount(10);
gridLayout.setRowCount(10);
for (int i = 1; i <= 100; i++) {
ImageView tile1 = new ImageView();
LayoutParams lp = new LayoutParams(100, 100);
tile1.setLayoutParams(lp);
lp.setMargins(4, 4, 4, 4); ////not working some error
tile1.setBackgroundColor(Color.rgb(255, 255, 5));
//tile1.setBackgroundResource(R.drawable.aalien);
//tile1.setBackgroundColor();
I have found a method setMargins(4, 4, 4, 4)
, but by this I can set the margin only for a ViewGroups
like LinearLayout
/ GridLayout
/ RelativeLayout
etc.
But I want to set margin for the ImageView
like I do in the xml
by using the android:layout_margin="5dp"
Try doing this for each ImageView
ImageView imageView = new ImageView(context);
GridLayout.LayoutParams params = (GridLayout.LayoutParams) imageView.getLayoutParams();
params.setMargins(4, 4, 4, 4); // left, top, right, bottom
imageView.setLayoutParams(params);
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.