Question

I am trying to get the LayoutParams from an ImageView that I have added to a RelativeLayout. The problem is when I call my method to get the LayoutParams it doesn't have any of the rules or margins I applied to it before.

In my code you see that I add either left or right alignment and various margins to get them to form a circle. But when I try to get the LayoutParams again, it has no rules or margins.

You can see my System.out.println() of each rule and it always says 0, although I have applied the left or right alignment to it.

Help appreciated, the code may look confusing. The method I am calling - swap() - is being called from the anonymous inner class as I need it to change the position each ImageView when it has been clicked.

How can I get the LayoutParams of another ImageView and apply the same alignment and margins to a different ImageView?

public class HelloMoonFragment extends Fragment {

     ArrayList<ImageView> imgViews = new ArrayList<ImageView>();
     View v;

        //Method I have created
    public int swap(int a){

    ArrayList<ImageView> copy = new ArrayList<ImageView>(imgViews);

    for(int i = 0; i < imgViews.size(); i++){
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(imgViews.get(i).getLayoutParams());

        int[] rules;
        rules = params.getRules();
        for(int x: rules){
            params.addRule(x);
            System.out.println("Rule: " + x); //Prints out 0??
        }

        v.findViewById(copy.get(i).getId()).setLayoutParams(params);
    }


    imgViews = copy;

    return 0;
}

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}


    //Initialises each ImageView and adds to ArrayList
public ImageView initializeImgViews(Activity a,final int id,final int imageResource1,final int imageResource2,int layoutFeature1,int marginLeft,int marginRight,int marginTop,int height){

    //Create new ImageView
    final ImageView im = new ImageView(a);

    //Set id & image source
    im.setId(id);
    im.setImageResource(imageResource1);

    //Set out Layout
    RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    params1.addRule(layoutFeature1);    
    params1.setMargins(marginLeft,marginTop,marginRight,10);
    im.setLayoutParams(params1);

    //Edit Image Height & Width
    im.getLayoutParams().height = height;
    im.getLayoutParams().width = height;

    //Set up Listener
    im.setTag("im1");
    im.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            swap(id);

            if (im.getTag().equals("im1")){

                im.setImageResource(imageResource2);
                im.setTag("im2");
            }
            else{
                im.setImageResource(imageResource1);
                im.setTag("im1");

            }               
        }
    });

    return im;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
    v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);

    RelativeLayout tl = (RelativeLayout) v.findViewById(R.id.l1);
    tl.setBackgroundColor(Color.RED);

    DisplayMetrics metrics = this.getResources().getDisplayMetrics();
    int width = metrics.widthPixels;


    int numberOfBells = 8;
    int HeightOfParent = 350;

    final float s = getActivity().getResources().getDisplayMetrics().density;
    int scale = (int) ((-20 + HeightOfParent/ ((int) (numberOfBells + 1)/2) * s + 0.5f));

    int pos = ((int) (width/2 - scale*1.2));
    int posX = (int) (HeightOfParent - scale * 0.6);

    double a = 0;
    double b = 0;


    for (int i = 0; imgViews.size() < numberOfBells;  i++ ){

        a = pos - pos * Math.sin(Math.toRadians((i) * 180/ (int) ((numberOfBells - 1 )/ 2)));
        b = posX - posX * Math.cos(Math.toRadians((i) * 180/ (int) ((numberOfBells - 1 )/ 2)));

        imgViews.add(initializeImgViews(getActivity(),(numberOfBells/2) - i,R.drawable.bell_dl_256,R.drawable.bell_ul_256,RelativeLayout.ALIGN_PARENT_LEFT,
                (int) a,0,(int) b,scale - (i)));

        imgViews.add(initializeImgViews(getActivity(),(numberOfBells/2) + i + 1,R.drawable.bell_dr_256,R.drawable.bell_ur_256,RelativeLayout.ALIGN_PARENT_RIGHT,
                0,(int) a,(int) b,scale + ((i + 1))));

    }

    for(ImageView i : imgViews){
        System.out.println("initialise b  "+i.getId());
        tl.addView(i);
    }

    tl.addView(txt);

    return v;
}

}

Était-ce utile?

La solution

ImageView i1 = (Imageview)findViewById(R.Id.img1);
LayoutParams lp = i1.getlayoutparams();

ImageView i2 = new ImageView(this);
i2.setLayoutParams(lp);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top