简体   繁体   中英

Android - changing margin issue?

I have one main linear layout which contains dynamically generated linear layouts. I am animating these layouts on ACTION_DOWN. But if i try to change the margin (top or bottom) of one layout, it affects other layouts as well. How to change one layouts margin without affecting others.

Any help would be rally appreciated.

Thanks.

EDIT

public View getLinearView() {
    ChildLinearLayout linearLayout = new ChildLinearLayout(context);
    LayoutParams layoutParams = new LayoutParams(500, 500);
    linearLayout.setLayoutParams(layoutParams);
    linearLayout.setBackgroundColor(Color.RED);
    layoutParams.setMargins(20, 20, 20, 20);
    layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
    TextView textView = new TextView(context);
    textView.setText("Hello World");
    linearLayout.addView(textView);
    System.out.println("View Added");
    views.add(linearLayout);
    return linearLayout;
}

I think since you are calling

layoutParams.setMargins(20, 20, 20, 20) 

for every dynamically generated layout, if you change these values then it will affect every one of your dynamically generated layouts.

Instead you could change your

getLinearView() method to getLinearView(int margin_value)

and every time you call this method for your dynamically generated layout, you could pass in the value you want for that particular layout.

ex: If I have 5 layouts and I want to set margin of first one to 50 and others to 20 then

for(int i=0;i<5;i++)
{
 if(i==0)
  view=getLinearView(50);
 else
  view=getLinearView(20);
}

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