3

我正在构建具有以下布局的应用程序:

一个RelativeLayout 容器有一个ImageView 子作为其背景,另外两个RelativeLayouts 作为页面的实际内容。

这些视图的规则设置如下:

// Define rules and params for views in the container
ImageView backgroundImg = new ImageView(this);
RelativeLayout.LayoutParams lpImg = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lpImg.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
backgroundImg.setImageResource(R.drawable.baggrund_smartphones_retina);
backgroundImg.setLayoutParams(lpImg);

RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.fragment_main_layout);
RelativeLayout storeLayout = (RelativeLayout)findViewById(R.id.fragment_store_layout);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(screenWidth, LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(screenWidth, LayoutParams.MATCH_PARENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp2.addRule(RelativeLayout.RIGHT_OF, R.id.fragment_main_layout);
mainLayout.setLayoutParams(lp1);
storeLayout.setLayoutParams(lp2);

root.removeAllViews();

//Add background first to ensure its in the back
snapView.addInfoPage(backgroundImg);
snapView.addInfoPage(mainLayout);

现在这是我的问题。但是我的 RelativeLayouts 与它们的父级正确对齐,但 ImageView 不是。相反,图像视图是这样的地方:

在此处输入图像描述

我究竟做错了什么?

4

2 回答 2

1

可能是您的图像尺寸小于您 parentLayout 的屏幕尺寸。

因此,如果您希望图像适合所有屏幕android:scaleType,请通过 xml 使用 imageView 标记中的属性:

<ImageView android:id="@+id/img"
blabla 
blabla 
android:scaleType="fitXY" />

或以编程方式:

imgView.setScaleType(ScaleType.FIT_XY);
于 2012-12-20T09:47:14.123 回答
0

像这样改变

RelativeLayout.LayoutParams lpImg = new RelativeLayout.LayoutParams(screenWidth, LayoutParams.MATCH_PARENT);

代替

RelativeLayout.LayoutParams lpImg = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

并像这样修复imageview

backgroundImg.setLayoutParams(ScaleType.FIT_XY);
于 2012-12-20T09:50:58.757 回答