简体   繁体   English

以编程方式将视图放置在RelativeLayout中

[英]Programmatically Position Views in RelativeLayout

I want my TextView to appear below my ImageView in a RelativeLayout that is going into a GridView. 我希望我的TextView出现在进入GridView的RelativeLayout中的ImageView下方。 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. 但是TextView总是显示在ImageView的顶部。

Check out this question . 看看这个问题

When you call rl.AddView(tv) , you should include the LayoutParams rl.AddView(tv, lp) . 调用rl.AddView(tv) ,应包括LayoutParams rl.AddView(tv, lp)

You have to use imageView.setLayoutParams(lp) in order to assign the params to your view. 您必须使用imageView.setLayoutParams(lp)才能将参数分配给视图。

How do you setLayoutParams() for an ImageView? 如何为ImageView设置setLayoutParams()?

Since my content is not dynamic, I worked around the issue by simply editing my image in Paint and placing text below the image. 由于我的内容不是动态的,因此我通过在Paint中编辑图像并将文本放置在图像下方来解决此问题。 Then I made that the background for the ImageView. 然后,我将其作为ImageView的背景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM