0

I need to programmatically align an ImageView to the bottom of a LinearLayout.

I, of course, already tried to add the ImageView as a child of the LinearLayout, but the image get shrinked (cause the LinearLayout is smaller than the image), and I need the image not be resized, only bottom-aligned.

I also tried this :

    RelativeLayout main_layout = (RelativeLayout) view.findViewById(R.id.main_layout);
    LinearLayout line_0_layout = (LinearLayout) view.findViewById(R.id.line_0_layout);

    ImageView horse_img = new ImageView(getActivity());
    horse_img.setImageResource(R.drawable.horse);

    RelativeLayout.LayoutParams layout_params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    layout_params.addRule(RelativeLayout.ALIGN_BOTTOM, line_0_layout.getId());

    horse_img.setLayoutParams(layout_params);
    main_layout.addView(horse_img);

but it doesn't work : the image is added to the view but not aligned to the LinearLayout.

It seems simple question, but it's not.

Thanks in advance for your help.

thomaus
  • 6,170
  • 8
  • 45
  • 63
  • Is `line_0_layout` a child of `main_layout`? Because the code you posted can only work if that is the case. ALIGN_BOTTOM is defined as a rule that aligns a child's bottom edge with another child's bottom edge. I don't think (but I'm not sure) that what you are trying to do is possible if `horse_img` and `line_0_layout` don't share a `RelativeLayout` parent. – Thijs Mergaert Oct 02 '13 at 09:44

1 Answers1

0
layout_params.addRule(RelativeLayout.BELOW, line_0_layout.getId());
Autocrab
  • 3,474
  • 1
  • 15
  • 15