0

我想通过编程方式在具有背景图像的相对布局中添加一个 imageView、TextView。我为相对布局指定了高度和宽度,但它不适合指定的宽度和高度。哪里出错了 ploesae help 在此先感谢这是我的代码:

RelativeLayout.LayoutParams lp_topheader = new RelativeLayout.LayoutParams(800,45);
    relative_topheader = new RelativeLayout(this);
    relative_topheader.setLayoutParams(lp_topheader);
    relative_topheader.setId(1);
    Resources resources_topheader = getResources();
    Drawable drawable_topheader = resources_topheader.getDrawable(R.drawable.headerbar_m);
    relative_topheader.setBackgroundDrawable(drawable_topheader);
    setContentView(relative_topheader);
RelativeLayout.LayoutParams lp_banner = new RelativeLayout.LayoutParams(385, 206);
    relative_banner = new RelativeLayout(this);
    relative_banner.setId(2);
    relative_banner.setLayoutParams(lp_banner);
    lp_banner.setMargins(40, 40, 0, 0);
    lp_banner.addRule(RelativeLayout.BELOW,1);
    ImageView iv = new ImageView(this);         
    iv.setScaleType(ScaleType.FIT_XY);
    iv.setLayoutParams(lp_banner);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.banner_image);
    iv.setImageBitmap(bitmap);
    setContentView(iv, lp_banner);
4

1 回答 1

2

根视图(即您设置为内容视图的视图)始终填充窗口的整个区域。如果您希望它只占用某个部分,请将其添加到另一个将占用整个窗口的布局中。

试试这个而不是最后一行:

LinearLayout ll = new LinearLayout(this);
ll.addView(iv, lp_banner);
setContentView(ll);
于 2013-05-21T11:11:37.950 回答