简体   繁体   English

Android在RelativeLayout中顶部和中央对齐图像

[英]Android align image in top and center in RelativeLayout

I'm trying to align image to top of my Relative Layout and after that center it horizontally. 我正在尝试将图像对齐到相对布局的顶部,然后将其水平居中。 Here is the code I'm using for : 这是我正在使用的代码:

<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"  />

,but this code is not working. ,但是此代码无法正常工作。 My image is centered,but not aligned at top of my Relative Layout. 我的图像居中,但未在“相对布局”的顶部对齐。 I tried with android:scaleType="fitStart" , but it's actually align imag left and top of it's parent. 我尝试使用android:scaleType="fitStart" ,但实际上是将imag左对齐,并将其顶部对齐。

So any idea how I can align the image correctly as I need? 那么,有什么想法可以根据需要正确对齐图像吗?

PS I forget to mention that I'm setting the image to my ImageView like this : PS我忘了提起我将图像设置为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);

code seems to be perfect except content area you had assign fill_parent takes whole view and will appear as center, so just modify it by wrap_content 代码似乎很完美,除了您已分配的内容区域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"  />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM