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

我正在尝试将图像与我的相对布局的顶部对齐,然后将其水平对齐.这是我正在使用的代码:
<ImageView  
        android:id="@+id/collection_image_background"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"  />

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

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

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

BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inTempStorage = new byte[8*1024];

    ops = BitmapFactory.decodeStream(cis,null,o2);
    ImageView view = (ImageView) findViewById(R.id.collection_image_background);
    view.setImageBitmap(ops);

解决方法

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

相关文章

ADB Remote ATV Android TV 的遥控器,基于 ADB Shell 命令 ...
使用Flutter自带的SearchDelegate组件实现搜索界面,通过魔改...
上篇文章讲解了怎么使用Kotlin的协程配合Retrofit发起网络请...
安卓开发——WebView+Recyclerview文章详情页,解决高度...
Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...