Android在RelativeLayout的顶部和中心对齐图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10209564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android align image in top and center in RelativeLayout
提问by Android-Droid
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?
那么知道如何根据需要正确对齐图像吗?
P.S. 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);
回答by Mohammed Azharuddin Shaikh
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" />