Android将图像放在RelativeLayout的顶部和中心

前端之家收集整理的这篇文章主要介绍了Android将图像放在RelativeLayout的顶部和中心前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试将图像与我的相对布局的顶部对齐,然后将其水平对齐.这是我正在使用的代码
  1. <ImageView
  2. android:id="@+id/collection_image_background"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:layout_alignParentTop="true"
  6. android:layout_centerHorizontal="true" />

,但此代码无效.我的图像居中,但未在我的相对布局顶部对齐.我尝试使用android:scaleType =“fitStart”,但它实际上是将图像左侧和顶部对齐的父级.

所以任何想法如何我可以正确对齐图像?

附:我忘了提到我正在将图像设置为我的ImageView,如下所示:

  1. BitmapFactory.Options o2 = new BitmapFactory.Options();
  2. o2.inTempStorage = new byte[8*1024];
  3.  
  4. ops = BitmapFactory.decodeStream(cis,null,o2);
  5. ImageView view = (ImageView) findViewById(R.id.collection_image_background);
  6. view.setImageBitmap(ops);

解决方法

代码似乎是完美的,除了你分配的内容区域fill_parent采用整个视图并将显示为中心,所以只需通过wrap_content修改
  1. <ImageView
  2. android:id="@+id/collection_image_background"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:layout_alignParentTop="true"
  6. android:layout_centerHorizontal="true" />

猜你在找的Android相关文章