1

我正在尝试LayoutParamsImageView我添加到RelativeLayout. 问题是当我调用我的方法来获取LayoutParams它时,它没有我之前应用的任何规则或边距。

在我的代码中,您会看到我添加了左对齐或右对齐以及各种边距以使它们形成一个圆圈。但是当我尝试LayoutParams再次获得时,它没有规则或边距。

你可以看到我System.out.println()的每条规则,它总是说 0,尽管我已经对其应用了左对齐或右对齐。

帮助表示赞赏,代码可能看起来令人困惑。我正在调用的方法swap()- 是从匿名内部类调用的,因为我需要它在每次单击它时更改位置ImageView

如何获得LayoutParams另一个ImageView并将相同的对齐和边距应用于不同的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;
}

}

4

1 回答 1

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

ImageView i2 = new ImageView(this);
i2.setLayoutParams(lp);
于 2013-11-05T10:52:53.577 回答