Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么此ImageView不能与其父视图正确对齐?_Android_Android Relativelayout - Fatal编程技术网

Android 为什么此ImageView不能与其父视图正确对齐?

Android 为什么此ImageView不能与其父视图正确对齐?,android,android-relativelayout,Android,Android Relativelayout,我正在构建一个具有以下布局的应用程序: RelativeLayout容器的背景是ImageView子级,页面的实际内容是另外两个RelativeLayout 这些视图的规则设置如下: // Define rules and params for views in the container ImageView backgroundImg = new ImageView(this); RelativeLayout.LayoutParams lpImg = new RelativeLayout.La

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

RelativeLayout容器的背景是ImageView子级,页面的实际内容是另外两个RelativeLayout

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

// 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没有。相反,imageview是这样的位置:


我做错了什么?

可能是您的图像尺寸小于父布局的屏幕尺寸

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

<ImageView android:id="@+id/img"
blabla 
blabla 
android:scaleType="fitXY" />
像这样改变

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);

你在版面里放了些填充物吗?没有。从未添加任何填充。是否确定图像没有透明边缘:)?
backgroundImg.setLayoutParams(ScaleType.FIT_XY);