0

I want my TextView to appear below my ImageView in a RelativeLayout that is going into a GridView. I've tried:

public override View  GetView(int position, View convertView, ViewGroup parent)
    {
        RelativeLayout rl = new RelativeLayout(context);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.FillParent);
        ImageView imageView;
        TextView tv;

        imageView = new ImageView(context);
        imageView.LayoutParameters = new GridView.LayoutParams(250, 250);
        imageView.SetScaleType(ImageView.ScaleType.CenterCrop);
        imageView.SetPadding(8, 8, 8, 8);
        imageView.SetImageResource(thumbIds[position]);
        imageView.Id = position;

        lp.AddRule(LayoutRules.Below, position);
        lp.AddRule(LayoutRules.CenterHorizontal);

        tv = new TextView(context);
        tv.Text = stringIds[position].ToString();
        tv.SetTextSize(Android.Util.ComplexUnitType.Dip, 20);
        tv.SetTextColor(Android.Graphics.Color.WhiteSmoke);
        tv.LayoutParameters = lp;

        rl.AddView(imageView);
        rl.AddView(tv);

        return rl;
    }

But the TextView always shows up on top of the ImageView.

jmease
  • 2,507
  • 5
  • 49
  • 89

3 Answers3

1

Check out this question.

When you call rl.AddView(tv), you should include the LayoutParams rl.AddView(tv, lp).

Community
  • 1
  • 1
Paul Burke
  • 25,496
  • 9
  • 66
  • 62
  • I'm not sure I follow. I'm not adding the same LayoutParameters for both views, I am only setting LayoutParameters (lp) on the TextView because that is the only one I care about position. I just want it under wherever the ImageView goes. – jmease Feb 08 '12 at 20:51
  • Oh, sorry, I read the question incorrectly. Updated my answer. – Paul Burke Feb 08 '12 at 20:54
  • Tried it. Doesn't seem to do anything differently from when I had tv.LayoutParameters = lp. Weird thing is now that I have two different ImageViews and 2 TextViews, the 2nd set shows up properly with the ImageView on top and the TextView below it. The first set still shows up with the two overlapping. – jmease Feb 08 '12 at 21:39
0

You have to use imageView.setLayoutParams(lp) in order to assign the params to your view.

How do you setLayoutParams() for an ImageView?

Community
  • 1
  • 1
0

Since my content is not dynamic, I worked around the issue by simply editing my image in Paint and placing text below the image. Then I made that the background for the ImageView.

jmease
  • 2,507
  • 5
  • 49
  • 89